Reputation: 219
I have a list of 100 objects. when a value of a property of the object is null I want to delete the whole object. Right now the if statement is not true, how can I fix this?
object:
{ id: 'blockstack-iou',
name: 'Blockstack (IOU)',
symbol: 'stx',
image: 'missing_large.png',
market_cap_usd: 174267825,
market_cap_change_percentage: null,
market_cap_relative_market_cap_percentage: 0 }
Code:
for (let i = 0; i < 100 ; i++) {
// console.log(object[i]);
console.log(Object.values(object[i]));
//TODO: if one property value of the data is null delete object
if (Object.values(object[i]) == null) {
console.log("Null detected and object deleted");
delete object[i];
}
}
Output of console.log(Object.values(object[i]));
[ 'blockstack-iou',
'Blockstack (IOU)',
'stx',
'missing_large.png',
174267825,
null,
0 ]
Upvotes: 0
Views: 64
Reputation: 931
You could do something like this.
const noNullPropertiesObjects = arrayWithObjects.filter(obj => !Object.values(obj).includes(null))
Upvotes: 1
Reputation: 21672
You could use filter()
on the original array to return items where Object.values(object[i])
doesn't include null
.
const object = [
{p1: 1, p2: 1},
{p1: 2, p2: null},
{p1: 3, p2: null},
{p1: 4, p2: 4}
];
const result = object.filter(o => !Object.values(o).includes(null));
console.log(result);
Upvotes: 3