How to select elements from an array based on the indices of another array in JavaScript?

Active3 hr before
Viewed126 times

8 Answers


Another way would be => a[aIndex]). If b is shorter than a this could also be faster. However if there are indexes in b that do not belong in a, you would end up with undefined "holes" in the array.,Asking for help, clarification, or responding to other answers.,This works, but I'm wondering if there is a better way to filter by index:, Is there a Mandarin counterpart for the Cantonese slang 做媒?

let a = [10, 20, 30, 40],
   b = [1, 3];

let res = [];
for (const entry of a.entries()) {
   if (b.includes(entry[0])) {

load more v

An array is an ordered list of values that you refer to with a name and an index.,Remember, JavaScript Array indexes are 0-based: they start at 0, not 1. This means that the length property will be one more than the highest index stored in the array:,Writing a value that is shorter than the number of stored items truncates the array. Writing 0 empties it entirely:,JavaScript typed arrays

let arr = new Array(element0, element1, ..., elementN)
let arr = Array(element0, element1, ..., elementN)
let arr = [element0, element1, ..., elementN]
load more v

The method executes the function once for each element present in the array:,every() does not execute the function for empty array elements.,The every() method returns true if all elements in an array pass a test (provided as a function).,If it finds an array element where the function returns a false value, every() returns false (and does not check the remaining values)

Definition and Usage

The every() method returns true if all elements in an array pass a test (provided as a function).

load more v

var arr = [1, 2, 3, 4],
   brr = [2, 4],
   res = arr.filter(f => !brr.includes(f));
load more v

You want to execute a particular script if a certain value exists in an array,If you are using modern browsers you may also use the includes() function instead of the indexOf() function,You want to avoid adding duplicate values to the array,However, instead of writing a loop for this case, you can use the inbuilt function of Array.indexOf () for the same case. If the value exists, then the function will return the index value of the element, else it will return -1


jQuery.inArray(search - value, array - or - string - in -which - to - search);
load more v

Take elements by matching the array and the index arrays,Take elements from an array along an axis.,The axis over which to select values. By default, the flattened input array is used.,Take elements using a boolean mask

Ni, Nk = a.shape[: axis], a.shape[axis + 1: ]
Nj = indices.shape
for ii in ndindex(Ni):
   for jj in ndindex(Nj):
   for kk in ndindex(Nk):
   out[ii + jj + kk] = a[ii + (indices[jj], ) + kk]
load more v

Find whether an array is subset of another array | Added Method 5,Search an element in a sorted and rotated array,Insert into the same set for the second array.,Traverse arr2[] and search for each element of arr2[] in the Hash Table. If element is not found then return 0.

load more v

An array, whether literal or object, can hold values of different data types:,Arrays in JavaScript are zero-based, which means the first element index is zero, and the last element is at the array length, minus 1:,Most JavaScript arrays use a numeric index, such as the following:,To create this in JavaScript using a multidimensional array, use the following code:

var arrObject = new Array("val1", "val2"); // array as object
var arrLiteral = ["val1", "val2"]; // array literal
var arrObject = new Array();
var arrObject = new Array("val1", "val2");
var arrLiteral = ["val1", "val2", "val3"];
someFunction("param1", ["val1", "val2"]);
function chgArray(arr) {
   arr[0] = "surprise!";

var newArray = new Array("val1", "val2");
var newLiteral = ["val1", "val2"];


alert(newArray); // prints surprise!,val2
alert(newLiteral); // prints surprise!,val2
var arrObject = new Array("val1", 34, true); // string, number, boolean
var arrLiteral = [arrObject, "val2", 18, false); // object, string, number, boolean
alert(arrLiteral); // prints out val1,34,true,val2,18,false
var arrObject = new Array();

arrObject[0] = "cat"; // array now has one element
alert(arrObject[0]); // prints cat
var farmAnimals = new Array("cat", "dog", "horse", "pig");
alert(farmAnimals[0]); // print cat
alert(farmAnimals[3]); // print pig
var arrLiteral = ["val1", , "val3"];
var largeCollection = new Array(100); // a new array with 100 undefined elements
load more v

Other "based-javascript" queries related to "How to select elements from an array based on the indices of another array in JavaScript?"