如何检查键在对象数组中是否为空?

时间:2019-03-28 09:02:11

标签: javascript arrays react-native

我正在对本机应用程序做出反应。而且我得到如下的json响应。

[
  {
    id: '508',
    class: 'class1',
    value: '0',
    percentage: '8.90',
    color: 'black'
  },
  {
    id: '509',
    class: 'class2',
    value: '0',
    percentage: '2.40',
    color: 'black'
  },
  {
    id: '510',
    class: 'class3',
    value: '0',
    percentage: '56.40',
    color: 'black'
  },
  {
    id: '511',
    class: 'class',
    value: '0',
    percentage: '2.40',
    color: 'black'
  }
]

我必须检查所有value属性是否为空。如果所有内容都为空,则必须执行一些操作,否则以其他操作为条件。

  const emptyValues = sortedData.map(value => value.key);
  console.log('emptyValues data', emptyValues);

2 个答案:

答案 0 :(得分:3)

使用Array.every()检查所有value键是否为'0'(或您对empty的定义是什么):

const data = [{"id":"508","class":"class1","value":"0","percentage":"8.90","color":"black"},{"id":"509","class":"class2","value":"0","percentage":"2.40","color":"black"},{"id":"510","class":"class3","value":"0","percentage":"56.40","color":"black"},{"id":"511","class":"class","value":"0","percentage":"2.40","color":"black"}]

const doEmptyAction = data.every(o => o.value === '0')

console.log(doEmptyAction)

答案 1 :(得分:-1)

JSInvokeable