简体   繁体   English

检测对象某个属性的值为空

[英]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.

相关问题 如何检查对象为null,然后检查属性为null,然后检查它们是否为null? - How to check object null and then property null and then assign value if they are null? 无法获取属性“ 2”的值:对象为null或未定义 - Unable to get value of the property '2' : object is null or undefined 值为 null 的对象属性被视为“” - Object property with the value of null is being treated as '' 属性“ gatherItemInfo”的值为null或未定义,不是Function对象 - The value of the property 'gatherItemInfo' is null or undefined, not a Function object Javascript:属性的值为null或未定义,不是Function对象 - Javascript: The value of the property is null or undefined, not a Function object 检查对象的任何属性是否具有空值 - Check if any property of object has null value 无法获取属性____的值:object为null或undefined - Unable to get value of the property ____: object is null or undefined 如果x = null,则将object属性赋值给value - Assign object property to value if x = null 属性&#39;addEvent&#39;的值为null或未定义,不是Function对象 - The value of the property 'addEvent' is null or undefined, not a Function object “ openNewWin”属性的值为null或未定义,不是Function对象 - The value of the property 'openNewWin' is null or undefined, not a Function object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM