jquery 获取不包含特定键的对象

kq0g1dla  于 2022-11-03  发布在  jQuery
关注(0)|答案(4)|浏览(132)

我有一个部分,用户可以在其中通过下拉选择筛选不同数据,
我有一个数据数组,如下所示。

const data = [
    {
        name: 'Trump',
        country: 'USA',
        age: 70,
        IQ: 140,
    },
    {
        name: 'ID Amini',
        country: 'Uganda',
        age: 50,
    },
    {
        name: 'Kenyatta',
        country: 'Kenya',
        age: 60,

    },
    {
        name: 'Obama',
        country: 'USA',
        age: 45,
        IQ: 141

    }
]

我的目标也是能够获取所有不包含IQ的对象,正如您在上面的数组中看到的,存在不包含IQ的对象。

现场演示:demo
以下是我目前所做的尝试。

let filter = ['empty']; // passed dynamically u can pass eg Obama, kenyata etc etc
let filterTyped ='IQ' // passed dynamically als here u can pass name, country etc etc

let filtredData = filter.forEach((item) =>{
    let dataFilter = data.find((element) =>{
        //return all objects which does not contain IQ if Item is empyt
        if(item === 'empty'){
            return element[filterTyped] ==undefined;
        }
        // return found object
        return element[filterTyped] ==item;
    })

    console.log('Filtred', dataFilter)
    if(!dataFilter) return;
})

有什么办法可以解决这个问题吗?

ao218c7q

ao218c7q1#

这是你要找的吗?

const data = [{
    name: 'Trump',
    country: 'USA',
    age: 70,
    IQ: 140,
  },
  {
    name: 'ID Amini',
    country: 'Uganda',
    age: 50,
  },
  {
    name: 'Kenyatta',
    country: 'Kenya',
    age: 60,

  },
  {
    name: 'Obama',
    country: 'USA',
    age: 45,
    IQ: 141

  }
]

let noIQ = [];
let hasIQ = [];

for (let key = 0; key < data.length; key++) {
  if (!("IQ" in data[key])) {
    noIQ.push(data[key]);

    // return single object for each entry
    object = Object.assign({}, ...noIQ)
    console.log("object", object)
  }

  if ("IQ" in data[key]) {
    hasIQ.push(data[key])
  }
}

console.log("noIQ:", noIQ)
/* console.log("hasIQ:", hasIQ) */
yvfmudvl

yvfmudvl2#

有两种方法可以实现这一点:
1.使用hasOwnProperty方法进行检查:
第一个
1.检查并解构:
只使用IQ!IQ作为返回值时要小心,因为像0这样的假值将是假的,即使对于保存数值的字段来说这是一个有效值。
第一个
一个破碎的例子:
第一个

a0x5cqrl

a0x5cqrl3#

使用filter()hasOwnProperty()的各种示例

const data = [
    {
        name: 'Trump',
        country: 'USA',
        age: 70,
        IQ: 140,
    },
    {
        name: 'ID Amini',
        country: 'Uganda',
        age: 50,
    },
    {
        name: 'Kenyatta',
        country: 'Kenya',
        age: 60,

    },
    {
        name: 'Obama',
        country: 'USA',
        age: 45,
        IQ: 141

    }
]

function doFilter(filter, filterTyped) {
  return data.filter(r => {
    if (filter[0] === 'empty') {
      if (!r.hasOwnProperty(filterTyped)) { return r }
    } else {
      if (r.hasOwnProperty(filterTyped) &&  filter.includes(r[filterTyped])) { return r }
    }
  })
}

let filter = ['empty']
let filterTyped ='IQ'
console.log( doFilter(filter, filterTyped) ) // ID Amini and Kenyatta

filter = [141]
filterTyped ='IQ'
console.log( doFilter(filter, filterTyped) ) // Obama

filter = [140, 141]
filterTyped ='IQ'
console.log( doFilter(filter, filterTyped) ) // Trump and Obama
6ljaweal

6ljaweal4#

参考你的代码片段,错误是forEach没有返回值。下面是一个伪代码,你可以把它做得更好。

let filter = ['empty']; // passed dynamically u can pass eg Obama, kenyata etc etc
let filterTyped ='IQ' // passed dynamically als here u can pass name, country etc etc

let filteredData = [];
filter.forEach((f) => {
  data.forEach((element) => {
        //return all objects which does not contain IQ if Item is empyt
        if(f === 'empty' && element.IQ === undefined) {
           filteredData.push(element);
        }
    })
})

console.log(filteredData);

相关问题