在LG中将标签应用于多维数组中的对象

时间:2015-07-13 17:10:11

标签: javascript arrays multidimensional-array

我有一个多维数组,并希望在眼睛颜色前面放置标签,例如"眼睛颜色:棕色"。问题是我不知道如何在循环中访问嵌套数组以将标签放在每个眼睛颜色旁边。我知道如何访问数组中的信息,例如people[0][3][0] = "blue";来改变第一个人的眼睛颜色。

我为这种困惑道歉。我正在关注这个视频教程https://www.youtube.com/watch?v=Cta5s1QBD8E和9:44状态的讲师使用for in循环,你可以添加头发和眼睛颜色的标签。

var people = [
  [ "James", 60, "United States", ["brown", "black"] ],
  [ "Patricia", 55, "United States", ["brown", "black"] ],
  [ "Patrice", 22, "United States", ["brown", "black"] ],
  [ "Montrell", 32, "United States", ["brown", "black"] ]
];
//people[0][3][0] = "pink";

// for loop that interates through people array   
for(var i = 0; i < people.length; i++) {
    // writes the Person and number header
     document.write("<h2>Person " + (i+1) + "</h2>");
   // loops through each person and writes their information
   for(var details in people[i]) {
       document.write(people[i][details] + "<br>");
   }
}

3 个答案:

答案 0 :(得分:2)

你真的应该使用对象而不是数组,例如

var people = [
  { 
    name : "James", 
    age : 60,
    country: "United States",
    eyeColor: "brown",
    hairColor: "black"
  },
  ...
];

使用方括号[]定义数组,它只是一个有序列表。对象使用花括号{}定义,并且是键值对的字典。

var james = people[0];

这将为您提供people数组中的第一个人。然后得到eyeColor:

var color = james.eyeColor;

答案 1 :(得分:0)

试试这个,

var people = [
  [ "James", 60, "United States", ["brown", "black"] ],
  [ "Patricia", 55, "United States", ["brown", "black"] ],
  [ "Patrice", 22, "United States", ["brown", "black"] ],
  [ "Montrell", 32, "United States", ["brown", "black"] ]
];
//people[0][3][0] = "pink";

// for loop that interates through people array
for(var i = 0; i < people.length; i++) {
// writes the Person and number header
 document.write("<h2>Person " + (i+1) + "</h2>");
   // loops through each person and writes their information
   for(var details in people[i]) {

   //checking if the current item under iteration is an object
   //if it is, it would be the ["brown", "black"] aray, then print
   //people[i][details][0] as hair color and people[i][details][1] as eye color
   if(typeof people[i][details] == "object") {
   document.write("hair color is "+ people[i][details][0] + "<br>");
   document.write("eye color is "+ people[i][details][1] + "<br>");
    }
    else{
         //the current item is either a name, age or country, of either String or Number type
          document.write(people[i][details] + "<br>");
         }

   }
}

如果数组的结构是统一的,并且array个眼睛是人数组中每个对象中的第四个项目。

如果您有任何疑惑,请阅读上一个for循环上方的评论。

如果您的目标是学习迭代多维数组,那么可以使用这样的数组,但是另一个答案所建议的对象结构是将来保存数据的理想方法。

答案 2 :(得分:0)

如果您正在寻找转换,请将现有人员数组数据转换为对象:

var people = [
  [ "James", 60, "United States", ["brown", "black"] ],
  [ "Patricia", 55, "United States", ["brown", "black"] ],
  [ "Patrice", 22, "United States", ["brown", "black"] ],
  [ "Montrell", 32, "United States", ["brown", "black"] ]
];

 for (var i = 0; i < people.length;  i++){
    var details = {};
    details.eyeColor = people[i][3][0];
    details.hairColor = people[i][3][1];
    people[i][3] = details;
    people[i].splice(4,1);
}

...