简体   繁体   English

按另一个对象的值过滤对象数组

[英]Filter array of objects by another object's values

I would like to filter an array of objects based on the values in another object.我想根据另一个 object 中的值过滤对象数组。 I am trying to map() the array and filter() inside the function so that I am able to get a new array with only the desired (active) fields.我正在尝试在 function 中map()数组和filter() ,以便我能够获得一个只有所需(活动)字段的新数组。

I am trying to get the filter on the Object.entries() , I try to compare the keys and check if the values of the active filters are true , but I am not getting it right.我正在尝试获取Object.entries()上的过滤器,我尝试比较键并检查有源过滤器的值是否为true ,但我没有做对。

 const records = [ { id: 1, name: "first", date: "05/02" }, { id: 2, name: "second", date: "06/02" }, { id: 3, name: "third", date: "07/02" }, { id: 4, name: "fourth", date: "08/02" } ]; const active = { id: true, name: false, date: true }; const result = records.map((record) => { return Object.entries(record).filter((entry) => { Object.entries(active).forEach((activeEntry) => { return activeEntry[1] && activeEntry[0] === entry[0]; }); }); }); console.log(result);

This is the desired outcome这是期望的结果

const desiredOutcome = [
  {
    id: 1,
    date: "05/02"
  },
  {
    id: 2,
    date: "06/02"
  },
  {
    id: 3,
    date: "07/02"
  },
  {
    id: 4,
    date: "08/02"
  }
];

You can filter over the entries of the object and convert it back to an object with Object.fromEntries .您可以过滤 object 的条目并将其转换回 object 与Object.fromEntries

 const records=[{id:1,name:"first",date:"05/02"},{id:2,name:"second",date:"06/02"},{id:3,name:"third",date:"07/02"},{id:4,name:"fourth",date:"08/02"}]; const active = { id: true, name: false, date: true }; const res = records.map(x => Object.fromEntries( Object.entries(x).filter(([k])=>active[k]))); console.log(res);

Simply filter the keys by the key existing and then use Object.fromEntries to go back to an object只需按现有密钥过滤密钥,然后使用Object.fromEntries到 go 回到 object

 const records = [ { id: 1, name: "first", date: "05/02" }, { id: 2, name: "second", date: "06/02" }, { id: 3, name: "third", date: "07/02" }, { id: 4, name: "fourth", date: "08/02" } ]; const active = { id: true, name: false, date: true }; const result = records.map((record) => { return Object.fromEntries( Object.entries(record).filter(([key,value]) => active[key])); }); console.log(result);

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

相关问题 按 object 属性过滤数组,然后将过滤对象之间的另一个属性的值加在一起 - Filter array by object property, then add together another property's values between the filtered objects 通过使用另一个数组的对象的值来增加数组对象的值 - Increment value of array object by using values of another array's objects 对象数组,某些对象包含另一个数组,需要通过包含数组的对象中的值对其进行过滤 - Array of objects with some objects containing another array, need to filter it by values within the object containing an array 对象数组按另一个数组值过滤 - Array of objects filter by another array values 用另一个对象数组过滤对象数组 - Filter array of objects with another array of object 过滤对象数组,其中一个对象的数组至少与另一个数组匹配 - Filter array of objects where one object's array has at least one match with another array 对象数组过滤并获取另一个结构中的值 - Array of objects filter and get values in another structure 针对Javascript中的另一个对象过滤对象数组 - Filter array of objects against another object in Javascript 根据来自另一个对象数组的多个值过滤对象数组 - Filter array of objects based on multiple values from another array of objects 用filter和一些对象过滤另一个对象数组 - filter an array of objects with another array of object with filter and some
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM