我该如何检查任何value
属性是否有值?
[
{
"name": "DESIGEMPRESA",
"value": ""
},
{
"name": "DSP_DIRECAO",
"value": ""
},
{
"name": "MES",
"value": ""
}
]
我该如何检查任何value
属性是否有值?
[
{
"name": "DESIGEMPRESA",
"value": ""
},
{
"name": "DSP_DIRECAO",
"value": ""
},
{
"name": "MES",
"value": ""
}
]
Array#some
来检查是否存在至少一个元素没有空的value
值。
var arr = [
{
"name": "DESIGEMPRESA",
"value": ""
},
{
"name": "DSP_DIRECAO",
"value": ""
},
{
"name": "MES",
"value": ""
}
],
res = arr.some(v => v.value != ''); //no object with not empty value
console.log(res);
const a = [
{
"name": "DESIGEMPRESA",
"value": ""
},
{
"name": "DSP_DIRECAO",
"value": ""
},
{
"name": "MES",
"value": ""
}
]
const r = a.map(x => _.isEmpty(x.value))
console.log(r)
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
您不需要使用lodash:
arr.some(obj => Boolean(obj.value))
var arr = [
{
"name": "DESIGEMPRESA",
"value": ""
},
{
"name": "DSP_DIRECAO",
"value": ""
},
{
"name": "MES",
"value": "d"
}
]
var out = arr.filter(function(obj){
return obj.value
})
console.log(out)
true/false
。 - kind user_.find(collection, [predicate=_.identity], [fromIndex=0])
https://lodash.com/docs/4.17.4#find。 let x = [
{
"name": "DESIGEMPRESA",
"value": "tr"
},
{
"name": "DSP_DIRECAO",
"value": ""
},
{
"name": "MES",
"value": ""
}
]
_.find(x, (o) => o.value.length > 0);
{name: "DESIGEMPRESA" , value: "tr"}
。