javascript:如何按年龄对员工对象数组进行排序

时间:2012-06-27 09:30:14

标签: javascript arrays sorting

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

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

但是,当我用一组数字替换employees数组时,代码工作正常,如: 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>

6 个答案:

答案 0 :(得分: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结构转换为text / html。

答案 1 :(得分: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。

答案 2 :(得分: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);

答案 3 :(得分: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;

答案 4 :(得分:0)

问题是document.getElementById("disp").innerHTML=employees; - 您将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);

答案 5 :(得分:0)

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

Example:

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;