admin 管理员组文章数量: 1086019
I have the following array of objects.
const abc = [
{
sku: 1,
features: ["Slim"],
fields: [
{ label: "Material", value: "Material1" },
{ label: "Type", value: "Type1" },
]
},
{
sku: 2,
features: ["Cotton"],
fields: [
{ label: "Material", value: "Material2" },
{ label: "Type", value: "Type1" },
]
},
{
sku: 3,
features: ["Cotton"],
fields: [
{ label: "Material", value: "Material3" },
{ label: "Type", value: "Type2" },
]
}
];
I want to filter only those objects whose features and fields value are present in this
const fieldsArr = ["Material1", "Material2", "Type1", "Slim"]
Expected Output is
let output = [
{
sku: 1,
features: ["Slim"],
fields: [
{ label: "Material", value: "Material1" },
{ label: "Type", value: "Type1" },
]
},
{
sku: 2,
features: ["Cotton"],
fields: [
{ label: "Material", value: "Material2" },
{ label: "Type", value: "Type1" },
]
},
]
I solved the features part like this
abc.forEach(e => {
if (e.features.some(v => fieldsArr.indexOf(v) !== -1)) {
output.push(e);
}
});
But I'm having problem with filtering the fields part. Is there a way to filter the objects based on the above condition in an optimized way.
I have the following array of objects.
const abc = [
{
sku: 1,
features: ["Slim"],
fields: [
{ label: "Material", value: "Material1" },
{ label: "Type", value: "Type1" },
]
},
{
sku: 2,
features: ["Cotton"],
fields: [
{ label: "Material", value: "Material2" },
{ label: "Type", value: "Type1" },
]
},
{
sku: 3,
features: ["Cotton"],
fields: [
{ label: "Material", value: "Material3" },
{ label: "Type", value: "Type2" },
]
}
];
I want to filter only those objects whose features and fields value are present in this
const fieldsArr = ["Material1", "Material2", "Type1", "Slim"]
Expected Output is
let output = [
{
sku: 1,
features: ["Slim"],
fields: [
{ label: "Material", value: "Material1" },
{ label: "Type", value: "Type1" },
]
},
{
sku: 2,
features: ["Cotton"],
fields: [
{ label: "Material", value: "Material2" },
{ label: "Type", value: "Type1" },
]
},
]
I solved the features part like this
abc.forEach(e => {
if (e.features.some(v => fieldsArr.indexOf(v) !== -1)) {
output.push(e);
}
});
But I'm having problem with filtering the fields part. Is there a way to filter the objects based on the above condition in an optimized way.
Share Improve this question asked Mar 11, 2020 at 9:47 beingyogibeingyogi 1,4162 gold badges17 silver badges31 bronze badges 1-
Why do you use
forEach
andpush
when you can usefilter
? – Christian Vincenzo Traina Commented Mar 11, 2020 at 9:54
2 Answers
Reset to default 7You need to iterate the nested arrays as well.
const
abc = [{ sku: 1, features: ["Slim"], fields: [{ label: "Material", value: "Material1" }, { label: "Type", value: "Type1" }] }, { sku: 2, features: ["Cotton"], fields: [{ label: "Material", value: "Material2" }, { label: "Type", value: "Type1" }] }, { sku: 3, features: ["Cotton"], fields: [{ label: "Material", value: "Material3" }, { label: "Type", value: "Type2" }] }], fieldsArr = ["Material1", "Material2", "Type1", "Slim"],
result = abc.filter(({ features, fields }) =>
features.some(v => fieldsArr.includes(v)) ||
fields.some(({ value }) => fieldsArr.includes(value))
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Use filter, and check each items bined values of features
and fields
(value) with fieldsArr.
const abc = [
{
sku: 1,
features: ["Slim"],
fields: [
{ label: "Material", value: "Material1" },
{ label: "Type", value: "Type1" }
]
},
{
sku: 2,
features: ["Cotton"],
fields: [
{ label: "Material", value: "Material2" },
{ label: "Type", value: "Type1" }
]
},
{
sku: 3,
features: ["Cotton"],
fields: [
{ label: "Material", value: "Material3" },
{ label: "Type", value: "Type2" }
]
}
];
const fieldsArr = ["Material1", "Material2", "Type1", "Slim"];
const res = abc.filter(item =>
[...item.features, ...item.fields.map(x => x.value)].some(fea =>
fieldsArr.includes(fea)
)
);
console.log(res);
// Update: more concise using destructure
const res2 = abc.filter(({features, fields}) =>
[...features, ...fields.map(({value}) => value)].some(fea =>
fieldsArr.includes(fea)
)
);
console.log(res2);
本文标签:
版权声明:本文标题:javascript - Filter array of objects if object contains attribute with value present in another array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744043133a2523625.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论