Below is the array of objects. what we have to do is to
- create an array that contains all the non-empty elements of array arr with value not equal to null, NaN, 'undefined' and 0
- remaining in the second array
var arr = [
{ id: 15 },
{ id: -1 },
{ id: 0 },
{ id: 3 },
{ id: 12.2 },
{},
{ id: null },
{ id: NaN },
{ id: "undefined" }
];
what I have tried is
var obj1 = {};
var prop1 = [];
var prop2 = [];
arr.forEach(el=>{
if(el.id!==0 || el.id!==null || el.id!==undefined || el.id!==NaN){
prop1.push(el)
}
else{
prop2.push(el)
}
})
console.log(prop1)
console.log(prop2)
but it is not working
output I receive -
1] [{id: 15}, {id: -1}, {id: 0}, {id: 3}, {id: 12.2}, {}, {id: null}, {id: null}, {id: "undefined"}]
2] []
expected -
1] [{id: 0}, {id: null}, {id: "undefined"}]
2] [{id: 15}, {id: -1}, {id: 3}, {id: 12.2}]
(x !== 0 || x != null)You are currently thinking about all things. ;) You likely intended&&instead of||.el.id!==0 || el.id!==null || el.id!==undefined || el.id!==NaNwill always be true? Consider:(x !== 1 || x !== 2)You meant&&."undefined"or did you just meanundefined? That seems a little bit weird.