admin管理员组

文章数量:1287830

I am just learning Javascript programming and I'm having issues in looping through an array of arrays. I need a coded procedure to go about it.

I want to print out each individual array in the array. I was trying to use the Map, but once type break it returns the key and value of the first array. I just need a code to help me print out each key and value of every array individually.

var arrOfArr = [
    ['one', 1],
    ['two', 2],
    ['three', 3]
]

var newmap = new Map(arrOfArr)
for (const [key, values] of newmap.entries()) {
    newmap.forEach((values, key )  => console.log(key, values))
} 

I am just learning Javascript programming and I'm having issues in looping through an array of arrays. I need a coded procedure to go about it.

I want to print out each individual array in the array. I was trying to use the Map, but once type break it returns the key and value of the first array. I just need a code to help me print out each key and value of every array individually.

var arrOfArr = [
    ['one', 1],
    ['two', 2],
    ['three', 3]
]

var newmap = new Map(arrOfArr)
for (const [key, values] of newmap.entries()) {
    newmap.forEach((values, key )  => console.log(key, values))
} 
Share Improve this question edited Jun 10, 2018 at 18:17 hygull 8,7402 gold badges46 silver badges55 bronze badges asked Jun 10, 2018 at 10:42 chukxychukxy 791 gold badge1 silver badge4 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 3

You can simply use a the .forEach method

The forEach() method executes a provided function once for each array element.

This way you can loop through arrOfArr and fill obj with the key/value pairs:

For each array element in arrOfArr you can select the key (first item in the sub array) with e[0] and it's value (second item in the sub array) with e[1].

Then write obj[e[0]] = e[1] to add a new key/value pair in obj


Here is the code:

var arrOfArr = [ ['one', 1], ['two', 2], ['three', 3] ];

const obj = {}
arrOfArr.forEach(e => {
  obj[e[0]] = e[1]
})

console.log(obj)


Or if you just want to print them individually, you need obj. Therefore use:

var arrOfArr = [ ['one', 1], ['two', 2], ['three', 3] ];

arrOfArr.forEach( e => console.log(`${e[0]} => ${e[1]}`) )

With ES6 Destructuring assignment you can achieve it with one line:

arrOfArr.forEach(([key, value]) => console.log(key, value));

First, have a look at the below attempt on Node REPL.

Reference: Using iterator on Map() to iterate over keys and values pairs

> var arrOfArr = [
...     ['one', 1],
...     ['two', 2],
...     ['three', 3]
... ]
undefined
>
> var newMap = new Map(arrOfArr)
undefined
> newMap
Map { 'one' => 1, 'two' => 2, 'three' => 3 }
>
> var iteratorObj = newMap[Symbol.iterator]();
undefined
>
> // Printing keys and values
undefined
> for (let item of iteratorObj) {
... console.log(item[0], item[1])
... }
one 1
two 2
three 3
undefined
>

Now, try it online.

var arrOfArr = [
    ['one', 1],
    ['two', 2],
    ['three', 3]
]

var newMap = new Map(arrOfArr)

var iteratorObj = newMap[Symbol.iterator]();

// Printing keys and values
for (let item of iteratorObj) {
	console.log(item[0], item[1])
}

arrOfArr is an Array which contains 3 Arrays.

JavaScript arrays are zero-indexed: the first element of an array is at index 0, and the last element is at the index equal to the value of the array's length property minus 1. Using an invalid index number returns undefined.

Useful link : MDN Array

Example:

var arrOfArr = [['one', 1],['two', 2],['three', 3]]

// First array INDEX 0
console.log("First: ", arrOfArr[0]);

// Second array INDEX 1
console.log("Second: ", arrOfArr[1]);

// Third array INDEX 2
console.log("Third: ", arrOfArr[2]);

本文标签: javascriptlooping through an array of arraysStack Overflow