+ 2
How can I print a nested array in console using javascript by using for of loop?
I want to print a array of string which contains the cities names line by line.
6 Antworten
+ 2
let array = [[11,12,13,14],[21,22,23,24,25],[31,32,33]];
for(let i=0;i<array.length;i++)
for(let j=0;j<array[i].length;j++)
console.log(array[i][j])
+ 2
Prashant Singh check it .
let array = [90,91,true,false,[11,12,13,14],[21,22,23,24,25],[31,32,33],["i","am","the","one"],"md","sayed","al","sahab"]
for(let i of array){
if (typeof(i)!="object"){
console.log(i)
}
else{
for(let j of i){
console.log(j)
}
}
}
+ 1
Using recursion and checking if element is array or not.
a=[1, 2,3,[4, 5]]
function print(arr){
for(let i of arr){
if(Array.isArray(i)){
print(i);
}
else{
console.log(i);
}
}
}
print(a);
+ 1
You need a nested loop.
For a 2-dimensional array, you could do:
const nested_list = [ ["a", "b"], ["c", "d" ]]
for (let item of nested_list) {
item.forEach(innerItem =>
console.log(innerItem))
}
You can do another for of loop instead of forEach as the inner loop if you wanted.
+ 1
let array = [[11,12,13,14],[21,22,23,24,25],[31,32,33]];
for(let i of array)
for(let j of i)
console.log(j)
0
let array = [[11,12,13,14],[21,22,23,24,25],[31,32,33]];
for(let i of array)
for(let j of i)
console.log(j)
It is right in case of numbers but If I want to print a array of string,it comes one alphabet at a time.