简体   繁体   中英

2d array with function javascript

I've task to create function that will display names from 2d array (const users). Each name should be displayed in separate line in console. Here is the code I've prepared but not sure if it's correct?

 const users = [["Jaydn Humphries", "Ayda Orozco"], ["Sanjeev Wilkinson", "Jorge Markham"]]; function print2DArray(array) { for(let i = 0; i < array.length; i++){ for(let j = 0; j < array.length; j++) { console.log(array[i][j]); } }return array; } print2DArray(users);

Your inner for loop should run on the inner array not on the outer array. ie j should be looped up to array[i].length not array.length

Although since in your example the inner arrays have the same length as the outer array, your example should work just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM