0

我正在尝试按年龄对员工对象数组进行排序,我得到以下输出:

[object Object],[object Object],[object Object],[object Object]

但是,当我将员工数组替换为以下数字数组时,代码可以正常工作: var points=[2,10,7,8]

有人可以告诉我我要去哪里错了吗?谢谢你。

以下是我的代码:

<script type="text/javascript">

function sortFunc(){

var employees=[]
 employees[0]={name:"George", age:32, retiredate:"March 12, 2014"}
 employees[1]={name:"Edward", age:17, retiredate:"June 2, 2023"}
 employees[2]={name:"Christine", age:58, retiredate:"December 20, 2036"}
 employees[3]={name:"Sarah", age:62, retiredate:"April 30, 2020"}

 employees.sort(function(a, b){
  return a.age-b.age
 // return a-b;
 })
 document.getElementById("disp").innerHTML=employees;

}

</script>
</head>
<body>
<div id="disp">Click the button below to sort.</div>
<button onclick="sortFunc()">Sort Now</button>
</body>
4

6 回答 6

1

排序工作正常。您输出它的方式不正确。

尝试使用循环而不是 innerHTML 将其添加到页面。

var out="";
for(var i=0;i<employees.length;i++){
  out+="<p>"+employees[i]['age']+", "+employees[i]['name']+"</p>\n";
}

document.getElementById("disp").innerHTML=out;

您也可以这样定义对象:

var employees=[
  {name:"George", age:32, retiredate:"March 12, 2014"},
  {name:"Edward", age:17, retiredate:"June 2, 2023"},
  {name:"Christine", age:58, retiredate:"December 20, 2036"},
  {name:"Sarah", age:62, retiredate:"April 30, 2020"}
]

干杯

G。

于 2012-06-27T09:40:00.383 回答
1

您的问题不在于排序,这是因为员工是一个对象数组,因此,无论是否排序,如果您将其转换为字符串表示形式以便将其添加到您将要获得的文档中

[object Object],[object Object],[object Object],[object Object]

要显示您的数据,您需要在将其转换为文本的方式上更加结构化。例如:

var html = "";
for(var i = 0; i < employees.length; i++) {
   var employee = employees[i];
   html = html + employee.name + " " + employee.age + "<br/>";
}
document.getElementById("disp").innerHTML=employees;

或者您可以考虑使用众多 javascript 模板库之一,这些模板库可以轻松地将复杂的 javascript 结构转换为文本/html。

于 2012-06-27T09:33:56.767 回答
0

您的排序功能确实有效。要查看输出,请尝试以下操作:

document.getElementById("disp").innerHTML='';
for(i=0; i<employees.length; i++) {
   document.getElementById("disp").innerHTML+=employees[i].age+'<br>';
}

代替

document.getElementById("disp").innerHTML=employees;
于 2012-06-27T09:35:54.780 回答
0

问题是document.getElementById("disp").innerHTML=employees;- 您在 HTML 中填充员工数组,而无需手动将其转换为有效的 HTML,因此您获得了 JavaScript 的默认文本表示形式,恰好是[object Object],[object Object],[object Object],[object Object].

试试这个:

function prepForHTML(emp) {
  var html='';
  for (var i=0;i<emp.length;i++) {
    for (var j in emp[i]) html+=j+"="+emp[i][j]+", ";
  }
  html+="<br>";
  return html;
}

document.getElementById("disp").innerHTML=prepForHTML(employees);
于 2012-06-27T09:39:44.000 回答
0

您需要覆盖toString()方法,默认会显示您[object Object]

例子:

function Employee(name, age, retiredate) {
  this.name = name;
  this.age = age;
  this.retiredate = retiredate;
}

Employee.prototype.toString = function() {
  return "Name:"+this.name+" Age:"+this.age+" Retiredate:"+this.retiredate;
};

var employees=[];
employees.push(new Employee("George", 32, "March 12, 2014"));
employees.push(new Employee("Edward", 17, "June 2, 2023"));
employees.push(new Employee("Christine", 58, "December 20, 2036"));
employees.push(new Employee("Sarah", 62, "April 30, 2020"));

employees.sort(function(a, b){
  return a.age-b.age;
});

document.getElementById("disp").innerHTML=employees;
于 2012-06-27T09:45:39.190 回答
0

你的方法是正确的。

但是,您的 JavaScript 缺少所有分号。另外,您应该使用push()而不是直接分配。

var employees = [];

employees.push( {name:"George", age:32, retiredate:"March 12, 2014"} );
employees.push( {name:"Edward", age:17, retiredate:"June 2, 2023"} );
employees.push( {name:"Christine", age:58, retiredate:"December 20, 2036"} );
employees.push( {name:"Sarah", age:62, retiredate:"April 30, 2020"} );

employees.sort(function(a, b){
  return a.age - b.age;
});

要输出可读的内容,首先将数据结构转换为字符串:

document.getElementById("disp").innerHTML = JSON.stringify(employees);
于 2012-06-27T09:34:11.630 回答