admin管理员组文章数量:1315309
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 ?
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 ?
- 1 Is it depending on Index? – Christheoreo Commented Jul 23, 2019 at 14:45
- Yes that is exactly what im looking for here – ShaneOG97 Commented Jul 23, 2019 at 14:46
- Will the two arrays always be the same length? – DBS Commented Jul 23, 2019 at 14:46
- Yes they will be the same – ShaneOG97 Commented Jul 23, 2019 at 14:47
-
What about just using
two.splice(1,2)
? – leonheess Commented Jul 23, 2019 at 14:47
6 Answers
Reset to default 4
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)
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);
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);
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;
});
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/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);
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]));
本文标签: JavaScript how to filter two arrays at the same timeStack Overflow
版权声明:本文标题:JavaScript how to filter two arrays at the same time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741978552a2408263.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论