Check if properties matter with lodash
5 answers
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)
+2
source to share
You can use Array#some
to check if at least one element has a null character 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);
+2
source to share
In lodash
there is _.isEmpty ()
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>
+1
source to share
You can use _.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);
returns {name: "DESIGEMPRESA" , value: "tr"}
+1
source to share