简体   繁体   English

如何在数组中过滤 object?

[英]How to filter object inside an array?

I want to check condition inside array with single object.我想用单个 object 检查数组内部的条件。

let arr = [
  {
    name: "john",
    description: {
      height: 5.5,
      weight: 54,
    },
  },
  {
    name: "mary",
    description: {
      height: 5.8,
      weight: 65,
    },
  },
  {
    name: "smith",
    description: {
      height: 6.1,
      weight: 85,
    },
  },
];

let obj = {
  height: 5.8,
  weight: 65,
};

i want to compare the obj within the array and if its match one, i want to get the name.我想比较数组中的 obj,如果它匹配一个,我想获取名称。 for ex, obj is equal to marry.例如,obj 等于结婚。 i want to print mary.我想打印玛丽。 This is what i have tried.这是我尝试过的。

let result = arr.filter((item) => item.description === obj )
console.log(result.name);

You could build a filter with the entries of the object and iterate all entries and check the values.您可以使用 object 的条目构建过滤器并迭代所有条目并检查值。 Then map the names.然后是 map 的名称。

 let array = [{ name: "john", description: { height: 5.5, weight: 54 } }, { name: "mary", description: { height: 5.8, weight: 65 } }, { name: "smith", description: { height: 6.1, weight: 85 } }], object = { height: 5.8, weight: 65 }, filters = Object.entries(object), result = array.filter(({ description }) => filters.every(([key, value]) => description[key] === value)).map(({ name }) => name); console.log(result);

You have to compare the desired keys against each other, rather than comparing the objects, as object equality is based upon their memory addresses being the same.您必须相互比较所需的键,而不是比较对象,因为 object 相等是基于它们的 memory 地址相同。

 let arr = [ { name: "john", description: { height: 5.5, weight: 54 } }, { name: "mary", description: { height: 5.8, weight: 65 } }, { name: "smith", description: { height: 6.1, weight: 85 } } ]; let obj = { height: 5.8, weight: 65 }; console.log( arr.filter(item => item.description.height === obj.height && item.description.weight === obj.weight) );

you can narrow down the the property that you want to filter on like this您可以像这样缩小要过滤的属性

  let arr = [{
      name: "john",
      description: {
        height: 5.5,
        weight: 54
      }
    },
    {
      name: "mary",
      description: {
        height: 5.8,
        weight: 65
      }
    },
    {
      name: "smith",
      description: {
        height: 6.1,
        weight: 85
      }
    }
  ];

  let obj = {
    height: 5.8,
    weight: 65
  };

  const filtered = arr.filter(({ description }) => description.height === obj.height && description.weight === obj.weight);
  console.log(filtered)

One approach would be using Object.keys .一种方法是使用Object.keys

 var arr = [ { name: "john", description: { height: 5.5, weight: 54, }, }, { name: "mary", description: { height: 5.8, weight: 65, }, }, { name: "smith", description: { height: 6.1, weight: 85, }}]; var obj = { height: 5.8, weight: 65 }; var result = arr.filter(({description})=>Object.keys(description).every(k=>description[k]==obj[k])).map(({name})=>name); console.log(result);

The root cause for the failure of filter filter condition is due to the comparison between objects being attempted.过滤器过滤条件失败的根本原因是由于正在尝试的对象之间的比较。

Comparison of objects can be done by using a helper function.可以使用帮助程序 function 来比较对象。

Here is the working exmaple:这是工作示例:

// array-filter-demo.js
let arr = [
    { name: "john", description: { height: 5.5, weight: 54 } },
    { name: "mary", description: { height: 5.8, weight: 65 } },
    { name: "smith",description: { height: 6.1, weight: 85 } }
  ]

let obj = { height: 5.8, weight: 65 }

// Helper function to compare objects
function matching(a, b) {
   return ( a.height === b.height && a.weight === b.weight)
}

// Call the helper function inside filter
const result = arr.filter(item => (matching(item.description, obj)))
console.log(result)

Output: Output:

$ node array-filter-demo.js 

[ { name: 'mary', description: { height: 5.8, weight: 65 } } ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM