I have an array of objects, where each object has certain properties. I want to remove those objects from the array which have empty strings assigned for all properties within the object.
Example :
var x = [{
"userName" : "XYZ",
"age": 22,
"gender": "M"
}, {
"userName" : "ABC",
"age": 23,
"gender": "M"
}, {
"userName" : "Mohan",
"age":"",
"gender": ""
}, {
"userName" : "",
"age":"",
"gender": ""
}]
Result should be
[{
"userName" : "XYZ",
"age": 22,
"gender": "M"
}, {
"userName" : "ABC",
"age": 23,
"gender": "M"
}, {
"userName" : "Mohan",
"age":"",
"gender": ""
}]
You can use a combination of filter
, every
and Object.values
:
const x = [{ "userName" : "XYZ", "age": 22, "gender": "M" }, { "userName" : "ABC", "age": 23, "gender": "M" }, { "userName" : "Mohan", "age":"", "gender": "" }, { "userName" : "", "age":"", "gender": "" }] const y = x.filter(z => !Object.values(z).every(w => w ==="")) console.log(y)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.