[英]Detect value of a property of the object is null
I have a list of 100 objects.我有一个包含 100 个对象的列表。 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?现在 if 语句不正确,我该如何解决这个问题?
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])); console.log(Object.values(object[i])) 的输出;
[ 'blockstack-iou',
'Blockstack (IOU)',
'stx',
'missing_large.png',
174267825,
null,
0 ]
You could use filter()
on the original array to return items where Object.values(object[i])
doesn't include null
.您可以在原始数组上使用filter()
来返回Object.values(object[i])
不包含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);
你可以做这样的事情。
const noNullPropertiesObjects = arrayWithObjects.filter(obj => !Object.values(obj).includes(null))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.