有没有办法在整个数组上检查对象的键值?

时间:2022-09-25 18:44:02

I am finding a way to check object key's value is empty or not in entire array, actually I am receiving lengthy array from an API which length is more than 1500 so I do not want run map function, or loop because array is too lengthy.

我找到一种方法来检查对象键的值是否为空整个数组,实际上我从一个长度超过1500的API接收冗长的数组,所以我不想运行map函数,或循环因为数组太长。

here is screenshot.

这是截图。

有没有办法在整个数组上检查对象的键值?

and here is an example of response what i get from API

以下是我从API获得的响应示例

[{
    reference: "1234",
    address: "abcz",
    city: "sydney",
    conso: "",
    date: "26/04/2017"
  },
  {
    reference: "1234",
    address: "abcz",
    city: "sydney",
    conso: "",
    date: "26/04/2017"
  },
  {
    reference: "1234",
    address: "abcz",
    city: "sydney",
    conso: "",
    date: "26/04/2017"
  },
  {
    reference: "1234",
    address: "abcz",
    city: "sydney",
    conso: "",
    date: "26/04/2017"
  },
  ....

]

3 个解决方案

#1


3  

You could use Array#some, which exits on the first find.

您可以使用Array#some,它会在第一次查找时退出。

var allEmpty = data.some(o => o[key] !== '');

or check for all, then use Array#every

或检查所有,然后使用Array#every

var allEmpty = data.every(o => o[key] === '');

#2


0  

Unclear from your question exactly what you want. If you only want to see if one object in the array has an empty 'conso' property then this will work without having to necessarily search through the whole array:

你的问题不清楚你究竟想要什么。如果您只想查看数组中的一个对象是否具有空的'conso'属性,那么这将无需搜索整个数组就可以工作:

const emptyConso = arr.find(place => !place.conso)

#3


0  

This is quite slow alas

这很慢

var ref = "1499"
console.log(JSON.stringify(obj).indexOf('"reference":"'+ref+'"')!=-1);

compared to

console.log(obj.some(o => o["reference"] === ref))

https://jsperf.com/some-or-stringify/

#1


3  

You could use Array#some, which exits on the first find.

您可以使用Array#some,它会在第一次查找时退出。

var allEmpty = data.some(o => o[key] !== '');

or check for all, then use Array#every

或检查所有,然后使用Array#every

var allEmpty = data.every(o => o[key] === '');

#2


0  

Unclear from your question exactly what you want. If you only want to see if one object in the array has an empty 'conso' property then this will work without having to necessarily search through the whole array:

你的问题不清楚你究竟想要什么。如果您只想查看数组中的一个对象是否具有空的'conso'属性,那么这将无需搜索整个数组就可以工作:

const emptyConso = arr.find(place => !place.conso)

#3


0  

This is quite slow alas

这很慢

var ref = "1499"
console.log(JSON.stringify(obj).indexOf('"reference":"'+ref+'"')!=-1);

compared to

console.log(obj.some(o => o["reference"] === ref))

https://jsperf.com/some-or-stringify/