1

How Can I get value from this array? I would like to take from each of Object value as firstPersonName.

enter image description here

I wrote something like this

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

But i am getting

enter image description here

Thanks for any help :)

1 Answer 1

3

This may help you:

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

or

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

Sign up to request clarification or add additional context in comments.

6 Comments

Bro, You have to add return in the callback function but your question don't have return :P
It doesn't work with return as well :( The result is the same :(
So I don't know why it still doesn't work. You might double check your array object. You may try console.log(email.length) It should return 2
It's returning 0. So it's not Array?
That makes sense, it's exactly array and it's empty array. when use map with empty array only return empty array. Please make sure the email contains any data. And also you can try to run code snippet in edited answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.