2

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


  }
}

how can I show most inner array element like contact Number in new line?

Tushar
  • 85,780
  • 21
  • 159
  • 179
Nan2015
  • 21
  • 4

3 Answers3

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