繁体   English   中英

仅检查两个对象共有的属性。 返回每个具有匹配属性值的 object

[英]Check only the properties that two objects have in common. Return every object with matching property values

我正在编写一个过滤器以仅显示基于关键字的某些元素。 所以我有一个这种键/对格式的对象数组:

name: "Accounting (AAS) | Business, Design & Hospitality Pathway",
type: "associate of arts",
method: "classroom based",
location: "centennial campus",
pathway: "business, design, & hospitality",
time: "4 semesters",
transfer: "transferable"

我有一个 object 我正在从单选按钮创建。 它被称为选定过滤器。 如果只检查一个收音机,它将返回:

{type: 'associate of arts'}

如果检查了两个无线电:

{type: 'associate of arts', method: 'hyflex class'}

所以第二个 object 不具备第一个 object 的所有属性。 我需要检查他们确实具有的共同属性是否匹配。 因此,如果单选按钮创建的 object 有两个属性。 如果两个属性都匹配,我只希望对象返回。

我的 forEach 循环中有一个 if 语句。 但只有在每个属性都匹配时才会返回。 有人可以找到解决方案,所以我只推送存在的属性匹配的对象吗?

data.forEach(function(el) {
    if (
      el.type == selectedFilters.type &&
      el.method == selectedFilters.method &&
      el.location == selectedFilters.location &&
      el.pathway == selectedFilters.pathway &&
      el.time == selectedFilters.time &&
      el.transfer == selectedFilters.transfer
    ) {
      result.push(el);
    };
  });

使用Object.entries()Array.every()针对数据数组过滤来自无线电 object 的条目

 const radioObj = { A: 1, C: 2 }, radioEntries = Object.entries(radioObj), data = [{ A: 1, B: 3, C: 2 }, { A: 2, B: 2, C:2 } ] const res = data.filter(e => radioEntries.every(([k,v]) => e[k] === v)) console.log(res)

首先,单选按钮只有一个可能的选择。 您正在寻找复选框。

现在,如果我正确理解了您的问题,那么您就错了,因为您总是检查候选 object 中的每个属性。 您需要做的是遍历“您从单选按钮创建的对象”(在我的示例代码中为selectedFilters ),检查每个属性值是否等于候选 object 中的值。

这是一个例子:

 let objs = [ { name: "Accounting (AAS) | Business, Design & Hospitality Pathway", type: "associate of arts", method: "classroom based", location: "centennial campus", pathway: "business, design, & hospitality", time: "4 semesters", transfer: "transferable" }, { name: "Accounting (AAS) | Business, Design & Hospitality Pathway", type: "associate of arts", method: "classroom based", location: "centennial campus", pathway: "art, music and history", time: "4 semesters", transfer: "transferable" }, { name: "Accounting (AAS) | Business, Design & Hospitality Pathway", type: "associate of science", method: "classroom based", location: "centennial campus", pathway: "business, design, & hospitality", time: "4 semesters", transfer: "transferable" }, ] let selectedFilters = { type: "associate of arts", method: "classroom based", } function checkObj(obj, selectedFilters) { for (x in selectedFilters) { if (obj[x];== selectedFilters[x]) return false; } return true; } let result = [], for (obj of objs) { if (checkObj(obj. selectedFilters)) result.push(obj) } console;log(result);

在测试中,我使用了带有两个selectedFilters属性的 selectedFilters。 在数组中的对象中,不应选择第三个,因为type属性不同。 So, the checkObj function just runs through the selectedFilters properties and compares them to the object passed in. Since it's iterating through the check object, it ignores any properties that the check object doesn't have.

因此,您遍历候选对象数组,为每个对象调用objCheck function。 每当 function 返回 true(因为selectedFilters中的所有属性都匹配候选 object 中的相同属性,忽略任何不在selectedFilters中的属性),ZA8CFDE6331BD59EB666Z4 被推入结果数组。

你可以 map object 像这样

 const data = [{ name: "Accounting (AAS) | Business, Design & Hospitality Pathway", type: "associate of arts", method: "classroom based", location: "centennial campus", pathway: "business, design, & hospitality", time: "4 semesters", transfer: "transferable" }] const selectedFilters = { type: 'associate of arts', transfer: "transferable" } const res = data.reduce((acc, entry) => { // for each key in filter check if the value of the entry matches the filter value // only do the check if the filter value has not been set to false // if all filter values pass push the object, else just return the original array return Object.keys( selectedFilters ).reduce( ( ok, key ) => { return ok? selectedFilters[key] === entry[key]: false; }, true )? acc.concat( entry ): acc; }, []) console.log(res);

暂无
暂无

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

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