只检查两个对象共有的属性。 Return 每个具有匹配 属性 值的对象
Check only the properties that two objects have in common. Return every object with matching property values
我正在编写一个过滤器以仅显示基于关键字的特定元素。所以我有一个 key/pair 格式的对象数组:
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"
我有一个从单选按钮创建的对象。它称为选定过滤器。如果只检查一台收音机,它将 return 这个:
{type: 'associate of arts'}
如果选中两个收音机:
{type: 'associate of arts', method: 'hyflex class'}
因此第二个对象不具有第一个对象的所有属性。我需要检查它们是否具有共同的属性匹配。所以如果单选按钮创建的对象有两个属性。如果两个属性都匹配,我只希望对象 return。
我的 forEach 循环中有一个 if 语句。但如果每个 属性 匹配,它只会 returns。有人可以找到解决方案,以便我只推送存在的属性匹配的对象吗?
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()
根据数据数组过滤无线电对象的条目
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)
首先,单选按钮将只有一个可能的选择。您正在寻找复选框。
现在,如果我正确理解你的问题,你就错了,因为你总是检查候选对象中的每个 属性。您需要做的是遍历“您从单选按钮创建的对象”(在我的示例代码中为 selectedFilters
),检查每个 those 的值是否属性等于候选对象中的值。
这是一个例子:
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
与两个选定的属性一起使用。在数组中的对象中,不应选择第三个,因为 type
属性 不同。因此,checkObj
函数只是遍历 selectedFilters
属性并将它们与传入的对象进行比较。由于它遍历检查对象,因此它会忽略检查对象不具有的任何属性。
因此,您遍历候选对象数组,为每个对象调用 objCheck
函数。每当函数 returns 为真时(因为 selectedFilters
中的所有属性都与候选对象中的相同属性匹配,忽略不在 selectedFilters
中的任何属性),对象被推入结果数组。
您可以像这样映射对象
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);
我正在编写一个过滤器以仅显示基于关键字的特定元素。所以我有一个 key/pair 格式的对象数组:
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"
我有一个从单选按钮创建的对象。它称为选定过滤器。如果只检查一台收音机,它将 return 这个:
{type: 'associate of arts'}
如果选中两个收音机:
{type: 'associate of arts', method: 'hyflex class'}
因此第二个对象不具有第一个对象的所有属性。我需要检查它们是否具有共同的属性匹配。所以如果单选按钮创建的对象有两个属性。如果两个属性都匹配,我只希望对象 return。
我的 forEach 循环中有一个 if 语句。但如果每个 属性 匹配,它只会 returns。有人可以找到解决方案,以便我只推送存在的属性匹配的对象吗?
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()
根据数据数组过滤无线电对象的条目
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)
首先,单选按钮将只有一个可能的选择。您正在寻找复选框。
现在,如果我正确理解你的问题,你就错了,因为你总是检查候选对象中的每个 属性。您需要做的是遍历“您从单选按钮创建的对象”(在我的示例代码中为 selectedFilters
),检查每个 those 的值是否属性等于候选对象中的值。
这是一个例子:
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
与两个选定的属性一起使用。在数组中的对象中,不应选择第三个,因为 type
属性 不同。因此,checkObj
函数只是遍历 selectedFilters
属性并将它们与传入的对象进行比较。由于它遍历检查对象,因此它会忽略检查对象不具有的任何属性。
因此,您遍历候选对象数组,为每个对象调用 objCheck
函数。每当函数 returns 为真时(因为 selectedFilters
中的所有属性都与候选对象中的相同属性匹配,忽略不在 selectedFilters
中的任何属性),对象被推入结果数组。
您可以像这样映射对象
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);