2017-01-26 208 views
1

如何从此数组中获取值?我想从每个Object值中取firstPersonName。Angular2从对象数组中获取值

enter image description here

我写了这样的事情

var rest = email.map(a => { 
    a.firstPersonEmail; 
}); 
console.log(rest); 

但我正在逐渐

enter image description here

感谢所有帮助:)

回答

3

这可以帮助你:

var rest = email.map(a => { 
    return a.firstPersonEmail; 
}); 
console.log(rest); 

var rest = email.map(a => a.firstPersonEmail); 
console.log(rest); 

const email = [ 
 
    {id:1, firstPersonEmail: "[email protected]"}, 
 
    {id:2, firstPersonEmail: "[email protected]"}, 
 
    {id:3, firstPersonEmail: "[email protected]"} 
 
]; 
 

 
const emptyEmail = []; 
 

 
console.log(`Email length: ${email.length}`); 
 
console.log(`Empty Email length: ${emptyEmail.length}`); 
 

 
const rest = email.map(a => a.firstPersonEmail); 
 
const emptyRest = emptyEmail.map(a => a.firstPersonEmail); 
 

 
console.log(rest); 
 
console.log(emptyRest);

+0

兄弟,你必须在回调函数中添加'return'但你的问题没有'return':P –

+0

一点也没有与工作回报以及:(结果是一样的:( – qwerty1234567

+0

所以我不知道为什么它仍然无法正常工作。你可以仔细检查你的数组对象。你可以试一下'console.log(email.length)'它应该返回2 –