从对象数组中获取每个名称和ID

时间:2019-09-10 06:33:17

标签: javascript arrays angular

我正在获取对象数组,我想从该数组中获取ID和名称,但不应使用foreach或任何其他循环来完成。这是我的数组

[{  companyId : "5cc6ca1b280cdf1522d1a3a4",     
    email : "abc+103@gmail.com",    
    firstName : "test",     
    inviteAccepted : false,
    lastName : "test",  
    _id : "5d512444955d441dfc52f640"     
 }, {   
    companyId : "5cc6ca1b280cdf1522d1a3a4",     
    email : "abc+104@gmail.com",    
    firstName : "test",     
    inviteAccepted : false,     
    lastName : "test", 
    _id : "5d528778e741722391d1fdd3"     
}]

我尝试过

var IdArray = users.map(function (el:any) { 
           return {
                    _id:el._id, 
                    sourceName:el.firstName+el.lastName
           }; 
    });

但是它将sourceName显示为undefined 我希望我的结果像这样

[{  sourceName : "test test",       
    _id : "5d512444955d441dfc52f640"     
  }, {  
    sourceName : "test test",       
    _id : "5d528778e741722391d1fdd3"     
}]

0 个答案:

没有答案