Reputation: 4562
I need some help with this, I have to filter arr objects by the elements of regions and years, I tried with filter method but I can't figure out how to achieve this, thanks in advance.
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 expectedOutput = [{ id: 1, region: ["china"], year: ["1990"] },
{ id: 2, region: ["china"], year: ["2010"] },
{ id: 3, region: ["taiwan"], year: ["1990"] },
{ id: 4, region: ["corea"], year: ["1990"] }];
Upvotes: 0
Views: 87
Reputation: 185
Please try this
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);
}
output:
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' ] }
Upvotes: 1
Reputation: 386560
Instead of taking a hard wired approach, you could take an object's entries as filters with the wanted key and possible values.
let array = [{ 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"] }],
region = ["china", "taiwan"],
year = ["1990"],
filters = Object.entries({ region, year }),
result = array.filter(o => filters.some(([k, a]) => a.some(v => o[k].includes(v))));
console.log(result);
Upvotes: 1
Reputation: 710
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"];
arr=arr.filter(obj => regions.includes(obj.region[0]) || years.includes(obj.year[0]));
console.log(arr);
Upvotes: 0
Reputation: 14904
Filter it
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(({region, year}) => region.some(el => regions.includes(el)) || year.some(year => years.includes(year)));
console.log(result);
Upvotes: 1