2017-08-31 37 views
-1

問題提示我創建一個程序,按姓氏對所有員工進行排序,並以表格格式將其打印到屏幕上。使用地圖列表實現數據。我的問題是我的控制檯只顯示數組中的所有元素。元素被排序,這是我需要的,但我不能只打印名稱。有任何想法嗎?在javascirpt中顯示已過濾的值

//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

你是如何做的「表格格式」? (該作業是可疑的不確定性) – Bergi

+0

tbh我不知道它是以表格格式表達的。我想它想要一些東西打印在桌子上。但我不是如何在JavaScript中做到這一點。我知道如何在PHP中實現它,這很容易實現。 –

+0

它在JS中非常相似,只是不會將它打印到控制檯,而是將其輸出到DOM。無論如何 - 你只需遍歷你的排序數組並放下名字。 – Bergi

回答

1

你不得不循環和日誌它顯示每個名稱:

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

高五!你擊中了要害。雖然你代表什麼? –

+0

@WalterPurvis用戶 – zfrisch

+0

就像是一個本地var? –