Angular2从对象数组中获取值

时间:2017-01-26 08:10:45

标签: javascript arrays object

我如何从这个数组获得价值?我想从每个Object值中取出firstPersonName。

enter image description here

我写了这样的东西

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

但我得到了

enter image description here

感谢您的帮助:)

1 个答案:

答案 0 :(得分: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: "John@mail.mail"},
  {id:2, firstPersonEmail: "John2@mail.mail"},
  {id:3, firstPersonEmail: "John3@mail.mail"}
];

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);