Javascript 对象数组,与其他数组比较并筛选

Javascript 对象数组,与其他数组比较并筛选,javascript,Javascript,我需要一些帮助,我必须通过区域和年份的元素过滤arr对象,我尝试了过滤方法,但我不知道如何实现这一点,提前感谢 设arr=[ {id:1,地区:[中国],年份:[1990]}, {id:2,地区:[中国],年份:[2010]}, {id:3,地区:[台湾],年份:[1990]}, {id:4,地区:[corea],年份:[1990]}, {id:5,地区:[corea],年份:[2010]}, ]; 让地区=[中国,台湾]; 让年份=[1990] 过滤 设arr=[ {id:1,地区:[中国

我需要一些帮助,我必须通过区域和年份的元素过滤arr对象,我尝试了过滤方法,但我不知道如何实现这一点,提前感谢

设arr=[ {id:1,地区:[中国],年份:[1990]}, {id:2,地区:[中国],年份:[2010]}, {id:3,地区:[台湾],年份:[1990]}, {id:4,地区:[corea],年份:[1990]}, {id:5,地区:[corea],年份:[2010]}, ]; 让地区=[中国,台湾]; 让年份=[1990] 过滤

设arr=[ {id:1,地区:[中国],年份:[1990]}, {id:2,地区:[中国],年份:[2010]}, {id:3,地区:[台湾],年份:[1990]}, {id:4,地区:[corea],年份:[1990]}, {id:5,地区:[corea],年份:[2010]}, ]; 让地区=[中国,台湾]; 让年份=[1990]; 让result=arr.filter{region,year}=>region.someel=>regions.includesel | | year.someyear=>years.includeYear; console.logresult 过滤

设arr=[ {id:1,地区:[中国],年份:[1990]}, {id:2,地区:[中国],年份:[2010]}, {id:3,地区:[台湾],年份:[1990]}, {id:4,地区:[corea],年份:[1990]}, {id:5,地区:[corea],年份:[2010]}, ]; 让地区=[中国,台湾]; 让年份=[1990]; 让result=arr.filter{region,year}=>region.someel=>regions.includesel | | year.someyear=>years.includeYear; console.logresult 设arr=[ {id:1,地区:[中国],年份:[1990]}, {id:2,地区:[中国],年份:[2010]}, {id:3,地区:[台湾],年份:[1990]}, {id:4,地区:[corea],年份:[1990]}, {id:5,地区:[corea],年份:[2010]}, ]; 让地区=[中国,台湾]; 让年份=[1990]; arr=arr.filterobj=>regions.includeObj.region[0]| |年。includeObj.year[0]; console.logarr 设arr=[ {id:1,地区:[中国],年份:[1990]}, {id:2,地区:[中国],年份:[2010]}, {id:3,地区:[台湾],年份:[1990]}, {id:4,地区:[corea],年份:[1990]}, {id:5,地区:[corea],年份:[2010]}, ]; 让地区=[中国,台湾]; 让年份=[1990]; arr=arr.filterobj=>regions.includeObj.region[0]| |年。includeObj.year[0];
console.logarr 您可以将对象的条目作为带有所需键和可能值的过滤器,而不是采用硬连线的方法

让数组=[{id:1,地区:[中国],年份:[1990]},{id:2,地区:[中国],年份:[2010]},{id:3,地区:[台湾],年份:[1990]},{id:4,地区:[corea],年份:[1990]},{id:5,地区:[corea],年份:[2010]}, 地区=[中国,台湾], 年份=[1990], filters=Object.entries{region,year}, result=array.filtero=>filters.some[k,a]=>a.somev=>o[k].includesv;
console.logresult 您可以将对象的条目作为带有所需键和可能值的过滤器,而不是采用硬连线的方法

让数组=[{id:1,地区:[中国],年份:[1990]},{id:2,地区:[中国],年份:[2010]},{id:3,地区:[台湾],年份:[1990]},{id:4,地区:[corea],年份:[1990]},{id:5,地区:[corea],年份:[2010]}, 地区=[中国,台湾], 年份=[1990], filters=Object.entries{region,year}, result=array.filtero=>filters.some[k,a]=>a.somev=>o[k].includesv; console.logresult 请试试这个

let arr = [
    { id: 1, region: ["china"], year: ["1990"] },
    { id: 2, region: ["china"], year: ["2010"] },
    { id: 3, region: ["taiwan"], year: ["1990"] },
    { id: 4, region: ["corea"], year: ["1990"] },
    { id: 5, region: ["corea"], year: ["2010"] }
  ];

let regions = ["china", "taiwan"];
let years = ["1990"];


let result = arr.filter((row) => {
    if (regions.includes(row.region[0]) ) {
        return row;
    } else if (years.includes(row.year[0])) {
        return row;
    }

});
console.log(result.length);
for (row of result) {
    console.log(row);
}
输出:

4
{ id: 1, region: [ 'china' ], year: [ '1990' ] }
{ id: 2, region: [ 'china' ], year: [ '2010' ] }
{ id: 3, region: [ 'taiwan' ], year: [ '1990' ] }
{ id: 4, region: [ 'corea' ], year: [ '1990' ] }
请试试这个

let arr = [
    { id: 1, region: ["china"], year: ["1990"] },
    { id: 2, region: ["china"], year: ["2010"] },
    { id: 3, region: ["taiwan"], year: ["1990"] },
    { id: 4, region: ["corea"], year: ["1990"] },
    { id: 5, region: ["corea"], year: ["2010"] }
  ];

let regions = ["china", "taiwan"];
let years = ["1990"];


let result = arr.filter((row) => {
    if (regions.includes(row.region[0]) ) {
        return row;
    } else if (years.includes(row.year[0])) {
        return row;
    }

});
console.log(result.length);
for (row of result) {
    console.log(row);
}
输出:

4
{ id: 1, region: [ 'china' ], year: [ '1990' ] }
{ id: 2, region: [ 'china' ], year: [ '2010' ] }
{ id: 3, region: [ 'taiwan' ], year: [ '1990' ] }
{ id: 4, region: [ 'corea' ], year: [ '1990' ] }

@好办法。但这并没有达到预期的效果。您的方法适用于所有地区和年份组合。谢谢@拉胡辛格,换个“好方法”。但这并没有达到预期的效果。您的方法适用于所有地区和年份组合。谢谢@拉胡辛,换一些