如何在Javascript对象数组中过滤数组对象?
我正在尝试通过产品筛选列表如何在Javascript对象数组中过滤数组对象?,javascript,arrays,typescript,filtering,Javascript,Arrays,Typescript,Filtering,我正在尝试通过产品筛选列表 const questions = [ { "id": 2616, "offerings": [{"code": "AA"},{"code": "AB"}]}, { "id": 1505, "offerings": [{"code": "AA"},{&qu
const questions = [
{ "id": 2616, "offerings": [{"code": "AA"},{"code": "AB"}]},
{ "id": 1505, "offerings": [{"code": "AA"},{"code": "ABC"}]},
{ "id": 1500, "offerings": [{"code": "AC"},{"code": "DC"}]}
];
const filterByArray = ['AB', 'DC'];
我的预期结果是根据FilterByRay中传递的内容返回所有数组元素
[
{ "id": 2616, "offerings": [{"code": "AA"},{"code": "AB"}]},
{ "id": 1500, "offerings": [{"code": "AC"},{"code": "DC"}]}
]
我尝试使用
var filtered = questions.filter(function(element) {
return element.offerings.filter(function(cd) {
return filterByArray.indexOf(cd.code) > -1;
}).length === filterByArray.length;
});
console.log(filtered)
但这会继续返回所有数组元素。使用和
const问题=[
{“id”:2616,“产品”:[{“代码”:“AA”},{“代码”:“AB”}]},
{“id”:1505,“产品”:[{“代码”:“AA”},{“代码”:“ABC”}]},
{“id”:1500,“产品”:[{“代码”:“AC”},{“代码”:“DC”}]}
];
常量filterByArray=['AB','DC'];
const output=questions.filter(q=>q.offices.some(o=>filterByArray.includes(o.code));
控制台日志(输出)代码>