2

I have a first array of values and a second array of indexes. I would like to filter the first array and return only values that have indexes in the second array.

So, given:

arr1 = [4775453877338112, 5901353784180736, 6605041225957376]
arr2 = [0,2]

I would like to return:

output = [4775453877338112, 6605041225957376]

Thanks. For bonus points, why doesn't the following work?

var output = arr1.filter( (item) => arr1.indexOf(item) in arr2 === true )

Apologies if this simple question is a duplicate of this: Filter array based on an array of index, but the underscore is throwing me off.

1
  • 1
    var output = arr1.filter( (item) => arr2.indexOf(arr1.indexOf(item)) >= 0 ) will work Seems most Effective Commented Feb 6, 2018 at 13:10

10 Answers 10

11

For bonus points, why doesn't the following work?

Because you are checking if the index is present in arr2, not the item itself.

Just simply use a map

var output = arr2.map( s => arr1[s] );

Demo

var arr1 = [4775453877338112, 5901353784180736, 6605041225957376];
var arr2 = [0,2];
var output = arr2.map( s => arr1[s] );
console.log( output );

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, the index, not the integer representing the index... Explains the weird results I was seeing. Thank you
If it's helpful to anyone, .map() accepts a second argument for the context of the callback, so the solution could be rewritten like this: var output = arr2.map(function (s) { return this[s]; }, arr1); if you want to define a reusable function rather than an anonymous function expression.
4

var output = arr1.filter( (item) => arr1.indexOf(item) in arr2 === true ) indexOf returns index of element not a boolean. You could use new Array.prototype.includes or test for value to be -1

Also if order is the same you could loop over index array instead.

const arr1 = [4775453877338112, 5901353784180736, 6605041225957376]
const arr2 = [0,2]

const output = arr2.map(i => arr1[i])

console.log(output)

Comments

3

A simple loop might be all you need:

arr1 = [4775453877338112, 5901353784180736, 6605041225957376];
arr2 = [0,2];
arr3 = [];

for (i = 0; i < arr2.length; i++) {
    arr3.push(arr1[arr2[i]]);
}

Result:

Array [ 4775453877338112, 6605041225957376 ]

2 Comments

well. guess we had the same intention
Sure, its the most simple and intentional approach ^^
2

Here's a solution without using an extra empty array and without nesting of the for loop. Also works in O(n) rather than O(n^2) suggested by some people here.

var arr1 = [4775453877338112, 5901353784180736, 6605041225957376];
var arr2 = [0,2];

var result = arr2.map(i => arr1[i]);

Comments

1

Your task is easier when writing a readable code instead of complex code:

var arr1 = [4775453877338112, 5901353784180736, 6605041225957376];
var arr2 = [0,2];
var output = [];

for (var i = 0; i < arr2.length; i++) {
  output.push(arr1[ arr2[i] ]);
}

console.log(output);

Comments

0

Try the following: `

let arr1 = [4775453877338112, 5901353784180736, 6605041225957376]
let arr2 = [0,2]

let result = arr1.filter(i => arr2.includes(arr1.indexOf(i)))
console.log(result);

`

1 Comment

I like it :D (nice answer there)
0

Try this

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script>
  $("document").ready(function(){
    arr1 = [4775453877338112, 5901353784180736, 6605041225957376]
arr2 = [0,2]
$.each( arr2, function( key, value ) {
  alert( arr1[value]);
});
    
  })
</script>  
</body>
</html>

1 Comment

having to use a whole libary for that task might be a little bit overload
0

Without using lodash this is a way to solve it. Do not use arr.forEach, it is slower than for.

const arr1 = [4775453877338112, 5901353784180736, 6605041225957376]
const arr2 = [0,2]
const finalArray = [];
let j = 0;
for (let i = 0, len = arr1.length; i < len; i++) {
  if(i==arr2[j]) {
    j++;
    finalArray.push(arr1[i]);
  }
}

1 Comment

You are overcomplicating it, just loop over the array with the requested indizies ^^
0

Modify the output variable as follows:

var output = arr1.filter((item) => arr2.includes(arr1.indexOf(item)));

Comments

-1

To be more specific irrespective of type of value, use reflection concept.

import java.lang.reflect.Array; . . .

.

. int[] arr1 = {1, 3, 7}; int[] arr2 = {0,2};

for (var i = 0; i < arr2.length; i++) { Object o=Array.get(arr1, arr2[i]);

System.out.println(o);

}

Output : 1 7

2 Comments

Welcome to Stack Overflow! Take a moment to read through the editing help in the help center. Formatting on Stack Overflow is different than other sites.
This question is for JavaScript, not Java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.