以列表形式获取多数组结果

时间:2016-03-15 19:28:41

标签: javascript

看看这段代码:

var people= ["jonh", "david", "brian", "paul", "martin", "scot", "steven", "george"];

var jonh= ["18", "male"];
var david = ["51", "male"];
var brian = ["13", "male"];
var paul = ["45", "male"];
var martin = ["10", "male"];
var scot = ["13", "male"];
var steven = ["15", "male"];
var george = ["20", "male"];

for (i = 0; i < people.length; i++) {
var yearsOld= ???;
document.write(yearsOld + '<br>');
}

我需要提供什么?得到一个人的年龄列表?

我试过像(people[i])[0]这样的东西但当然不行!

帮助?

1 个答案:

答案 0 :(得分:0)

将年龄数据包装在如下对象中:

var people = ["jonh", "david", "brian", "paul", "martin", "scot", "steven", "george"];

var data = {
  jonh: ["18", "male"],
  david: ["51", "male"],
  brian: ["13", "male"],
  paul: ["45", "male"],
  martin: ["10", "male"],
  scot: ["13", "male"],
  steven: ["15", "male"],
  george: ["20", "male"]
};

for (i = 0; i < people.length; i++) {
  var person = people[i];
  var yearsOld = data[person][0];
  document.write(yearsOld + '<br>');
}