Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

single iteration over multidimensional javascript array

for example:

var myArray = [
 [id1, "john", 30],
 [id2, "smith", 60],    
 [id3, "kate", 90],
];

I would like with one loop to get data based on position in array

I need to get something like :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

john is 30

smith is 60

kate is 90

>Solution :

You could always just iterate to get a reference for placement in the array and then hardcode values into the code regarding values inside that reference.

var myArray = [
 ["john", 30],
 ["smith", 60],    
 ["kate", 90],
];

for (let i = 0; i < myArray.length; i++) {
  console.log(`${myArray[i][0]} is ${myArray[i][1]}`)
}

If you want to get them in the same line, use a string.

var myArray = [
 ["john", 30],
 ["smith", 60],    
 ["kate", 90],
];

let str = ''
for (let i = 0; i < myArray.length; i++) {
  str += `${myArray[i][0]} is ${myArray[i][1]} `
}
console.log(str)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading