var people = [
["nan", "30", "female", ["4545345454", "4324324324"]],
["ban", "35", "male", ["4324234234", "7576343243"]],
["san", "38", "male", ["3242342343", "3423423434"]]
];
//var k=0;
// document.write(people[0][3][0]);
for (var row = 0; row < people.length; row++) {
document.write("<h2> Person" + (row + 1) + "</h2>");
for (var col = 0; col < people[row].length; col++) {
document.write(people[row][col] + "<br>");
}
}
Asked
Active
Viewed 222 times
2
-
Is people array containing just array elements or it is a mix of types ? – ismnoiet Oct 13 '15 at 23:07
3 Answers
0
var people =[
["nan","30","female", ["4545345454", "4324324324"]],
["ban","35","male", ["4324234234", "7576343243"]],
["san", "38","male", ["3242342343", "3423423434"]]
];
//var k=0;
// document.write(people[0][3][0]);
for(var row=0;row<people.length;row++){
document.write("<h2> Person" +(row+1)+"</h2>");
for(var col=0; col<people[row].length-1; col++){
document.write(people[row][col] +"<br>");
}
for(var pcol=0; pcol<people[row][3].length; pcol++){
document.write(people[row][3][pcol] +"<br>");
}
}
you need to loop the phone array

Benjaco
- 303
- 3
- 11
-
Thank you very much for your answers.surely we need one look for phone and I was putting that code in col loop and was not work. can you explain me? – Nan2015 Oct 13 '15 at 23:22
-
0
You can do this dynamically if you feel like you're going to have other elements in your objects that also might be arrays:
function printArray(array) {
for (var key in array) {
if (typeof(array[key]) === "object") {
printArray(array[key]);
} else {
document.write(array[key] + "<br>");
}
}
}
for (var i = 0; i < people.length; i++) {
document.write("<h2> Person" +(i+1)+"</h2>");
printArray(people[i]);
}
This looks through your entire object structure, finds the elements that are of type object
, and recursively look through those objects as well.

David Li
- 1,250
- 8
- 18
-
This is a direct copy of another answer from link above. Don't directly copy answers without giving link to source – charlietfl Oct 13 '15 at 23:56
-
This is actually my handwritten solution... I don't copy answers. I guess it just happened to be a coincidence. – David Li Oct 14 '15 at 00:03
-
@charlietfl I took a look at the link and there are mindblowing similarities. However if you look closely these solutions are in fact different, with a different type of loop and multiple calls to the recursive function. Just wanted to sort that out because I don't like being accused of such :( – David Li Oct 14 '15 at 00:07
0
You can use recursion, in case the depth is unknown
var people = [
["nan", "30", "female", ["4545345454", "4324324324"]],
["ban", "35", "male", ["4324234234", "7576343243"]],
["san", "38", "male", ["3242342343", "3423423434"]]
];
var dump = function(o) {
if (o instanceof Array) {
for (var i = 0, l = o.length; l > i; i++)
dump(o[i]);
return;
}
document.write(o + "<br/>");
};
dump(people);