在javascript中显示过滤的值

时间:2017-08-31 16:34:35

标签: javascript arrays

问题是我创建了一个程序,按姓氏对所有员工进行排序,并以表格格式将它们打印到屏幕上。使用地图列表实施数据。我的问题是我的控制台只显示数组中的所有元素。元素被排序,这是我需要的,但我不能只打印名称。有什么想法吗?

//global array of employee list
var users = [
    {firstname : "Jacquelyn", lastname: "Jackson",    position:"DBA",               separationDate:""},
    {firstname : "John",      lastname: "Johnson",    position:"Manager",           separationDate:"2016-12-31"},
    {firstname : "Sally",     lastname: "Weber",      position:"Web Developer",     separationDate:"2015-12-18"},
    {firstname : "Michaela",  lastname: "Michaelson", position:"District Manager",  separationDate:"2015-12-19"},
    {firstname : "Jake",      lastname: "Jacobson",   position:"Programmer",        separationDate:""},
    {firstname : "Tou",       lastname: "Xiong",      position:"Software Engineer", separationDate:"2016-10-05"}
];

//compare first name, puts array in order
 users.sort(function(a,b){
    return a.firstname.localeCompare(b.firstname);
});

console.log(users.sort());

1 个答案:

答案 0 :(得分:1)

您必须循环并记录它才能显示每个名称:

users.forEach(u => console.log(u.firstname));