0

获取以下值的最容易理解的解决方案是什么:

Bill, Smith, 32
Sarah, Lord, 28

我只做到了这一点:

HTML

<a href="#" onclick="displayEmployees();">displayEmployees</a>

Javascript

function displayEmployees() {
var employees = new Array();
employees['firstName'] = new Array('Bill','Sarah','Elizabeth','David','Robert');
employees['lastName'] = new Array('Smith','Lord','Crayford','Clark','Robinson');
employees['age'] = new Array('32','28','20','54','18');
for(x in employees) {
    var value = employees[x]
    document.write(value + "<br />");
}
4

3 回答 3

2
function displayEmployees() {
var employees = new Array();
employees['firstName'] = new Array('Will','Sarah','Elizabeth','David','Robert');
employees['lastName'] = new Array('Smith','Lord','Crayford','Clark','Pattinson');
employees['age'] = new Array('32','28','20','54','18');
for(var i=0;i<employees['firstName'].length;i++)             //can be employees['lastName']or employee['age'] too 
{
    document.write(employees['firstName'][i]+','+employees['lastName'][i]+','+employees['age'][i]+'<br />');}
}
于 2013-06-18T20:42:30.853 回答
2
var array = [
    ["Bill", "Smith", "32"],
    ["Sarah", "Lord", "28"],
    ["Elizabeth", "Crayford", "20"],
    ["David", "Clark", "54"],
    ["Robert", "Robinson", "18"]
];
for (var i = 0, len = array.length; i < len; ++i) {
    document.write(array[i].join(", ") + "<br>");
}
于 2013-06-18T20:33:20.847 回答
1

你可以这样做

var value;
for(x in employees['firstName']){
   value = employees['firstName'][x]+', '
          +employees['lastName'][x]+', '
          +employees['age'][x];

   document.write(value + "<br />");
}

更好的方法是以其他方式创建员工数组,像这样

employees = [{'firstName':'Bill','lastName':'Smith','age':32},
             {'firstName':'Sarah','lastName':'Lord','age':28},...];

var value;
for(x in employees){
   value = employees[x].join(', ');
   document.write(value + "<br />");
}
于 2013-06-18T20:51:42.420 回答