Index an array of arrays with an array of indexes in javascript

Active3 hr before
Viewed126 times

8 Answers


This chapter introduces collections of data which are ordered by an index value. This includes arrays and array-like constructs such as Array objects and TypedArray objects.,concat() joins two or more arrays and returns a new array.,An array is an ordered list of values that you refer to with a name and an index.,slice(start_index, upto_index) extracts a section of an array and returns a new array.

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

Use .map:

let resultArr = => fruitier[i])
load more v

Note that the (index) column is for the illustration that indicates the indices of the inner array.,Multidimensional Array,For example, to add a new element at the end of the multidimensional array, you use the push() method as follows:,You can use the Array methods such as push() and splice() to manipulate elements of a multidimensional array.

To declare an empty multidimensional array, you use the same syntax as declaring one-dimensional array:

.wp - block - code {
      border: 0;
      padding: 0;

   .wp - block - code > div {
      overflow: auto;

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;

   .hljs {
      box - sizing: border - box;

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
let activities = [];
Code language: JavaScript(javascript)
load more v

You access an array element by referring to the index number:,An array can hold many values under a single name, and you can access the values by referring to an index number.,With JavaScript, the full array can be accessed by referring to the array name:,These two different statements both create a new array containing 6 numbers:

const cars = ["Saab", "Volvo", "BMW"];
let x = ;

An array in JavaScript is a type of global object that is used to store data. Arrays consist of an ordered collection or list containing zero or more data types, and use numbered indices starting from 0 to access specific items.,There are two ways to create an array in JavaScript:, Understanding Comparison and Logical Operators in JavaScript ,An array in JavaScript is a type of global object used to store data. Arrays can store multiple values in a single variable, which can condense and organize our code. JavaScript provides many built-in methods to work with arrays, including mutator, accessor, and iteration methods.

// Assign the five oceans to five variables
const ocean1 = "Pacific";
const ocean2 = "Atlantic";
const ocean3 = "Indian";
const ocean4 = "Arctic";
const ocean5 = "Antarctic";
load more v

To read an Array element, you put an index in square brackets (indices start at zero):,To change an Array element, you assign to an Array with an index:,The Array literal starts and ends with square brackets []. It creates an Array with three elements: 'a', 'b', and 'c'.,Returns a new Array containing the elements of the receiver whose indices are between (including) start and (excluding) end.

const arr = ['a', 'b', 'c'];
load more v

Alongside the plain object, the array is a widely used data structure in JavaScript. And a widely used operation on arrays is accessing elements by index.,The usual way to access an array element by index is the use of square brackets array[index]:,In simple words, accesses the element at index argument.,Here’s a more detailed example of how method accesses elements:

The usual way to access an array element by index is the use of square brackets array[index]:

javascriptconst fruits = ['orange', 'apple', 'banana', 'grape'];
const item = fruits[1];
item; // => 'apple'
load more v

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:,Use an associative array to store the elements, using the element identifiers as array index:,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 "arrays-javascript" queries related to "Index an array of arrays with an array of indexes in javascript"