7

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))
} 
0

4 Answers 4

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]}`) )

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

3 Comments

lets say i just want to console.log() the second array in the arrays, how do i go about that??
You mean 1, 2 and 3?
If you want to output ['two', 2] then write arrOfArr[1]
1

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

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

Comments

0

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])
}

2 Comments

lets say i just want to console.log() the second array in the arrays, how do i go about that??
Map { 'one' => 1, 'two' => 2, 'three' => 3 } is not indexed like arrays so it is hard to directly access any of its items. Read "It should be noted that a Map which is a map of an object, especially a dictionary of dictionaries, will only map to the object's insertion order—which is random and not ordered." from developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. So in this case it's better if you will use console.log(arrOfArr[1]); to print second array.
0

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]);

Comments

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.