Reputation: 530
Say I have two arrays:
var one = [1,2,3,4,5];
var two = ["A","B","C","D","E"];
If i were to filter the first array so that it returns this result:
var resultOne = one.filter(v => v == 2 || v == 3 );
The result would return [2,3]
but how would I filter the other array to return [B,C]
... based on the result of this first one ?
Upvotes: 2
Views: 4170
Reputation: 1258
A good old for loop or use the second parameter of the filter callback which gives you the current array index. Simply push the corresponding element of two
onto resultTwo
by index.
var resultTwo = [];
var resultOne = one.filter((v, i) => {
var isMatch = v == 2 || v == 3;
if (isMatch) resultTwo.push(two[i]);
return isMatch;
});
Upvotes: 1
Reputation: 73
Not the most efficient, but this is the most general and readable way I can think of to filter multiple corresponding arrays:
var a = [1, 2, 3, 4, 5];
var b = ["A", "B", "C", "D", "E"];
var indices = [];
for (var i = 0; i < a.length; i++) {
var v = a[i];
if (v == 2 || v == 3) {
indices.push(i);
}
}
console.log(indices.map(i => a[i]));
console.log(indices.map(i => b[i]));
Upvotes: 0
Reputation: 12984
You can use Array.reduce()
:
var one = [1,2,3,4,5];
var two = ["A","B","C","D","E"];
const [resultOne, resultTwo] = one.reduce((acc, v, i) => (v == 2 || v == 3) ? (acc[0].push(v), acc[1].push(two[i]), acc) : acc, [[], []]);
console.log(resultOne);
console.log(resultTwo);
Or use this version if the two arrays haven't the same length:
var one = [1,2,3,4,5,6];
var two = ["A","B","C","D","E"];
const [resultOne, resultTwo] = one.reduce((acc, v, i) => (v == 2 || v == 6) ? (acc[0].push(v), (i in two) ? acc[1].push(two[i]) : '', acc) : acc, [[], []]);
console.log(resultOne);
console.log(resultTwo);
Upvotes: 1
Reputation: 68923
You can implement filter on the array by passing the index, return if index + 1
includes in the resulted array.
Please Note: Since the solution is based on the index, it will not work for the random numbers of array (not sequential).
var one = [1,2,3,4,5];
var two = ["A","B","C","D","E"];
var resultOne = one.filter(v => v == 2 || v == 3 );
var resultTwo = two.filter((v,i) => resultOne.includes(i+1));
console.log(resultOne);
console.log(resultTwo);
OR: In Single line:
var one = [1,2,3,4,5];
var two = ["A","B","C","D","E"];
var result = two.filter((v,i) => one.filter(v => v == 2 || v == 3).includes(i+1));
console.log(result);
Upvotes: 2
Reputation: 2171
EDIT: Just realized somone posted the same answer... The only thing I can add is that you need to subtract 1 from your original array to arrive at the correct index.
If you use map you can map the results from your filtering. This is assuming the result will always be an index for your second collection.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
var one = [1,2,3,4,5];
var two = ["A","B","C","D","E"];
var resultOne = one.filter(v => v == 2 || v == 3 );
var resultTwo = resultOne.map((indexFromResultOne) => two[indexFromResultOne - 1]);
// Because you aren't using a zero based index.
console.log(resultTwo);
Upvotes: 0
Reputation: 1833
var one = [1,2,3,4,5];
var two = ["A","B","C","D","E"];
var resultOne = two.filter((v,i) => one[i] == 2 || one[i] == 3 );
console.log(resultOne)
Upvotes: 4