0

I have the following javascript array var rows.

[
    { email: '[email protected]' },
    { email: '[email protected]' }
]

Of which, I wanted to make it just...

[
    '[email protected]',
    '[email protected]'
]

So, I tried to loop it using forEach

rows.forEach(function(entry) {
    console.log('entry:');
    console.log(entry.email);
});

However, I am stuck with that.

entry:
[email protected]
entry:
[email protected]

Any help is appreciated.

2
  • Note: You already have the question tagged with node.js and javascript, so they don't need to be listed again in the title. Commented Jul 19, 2013 at 8:30
  • Alright. Noted Jonathan. Commented Jul 19, 2013 at 8:30

2 Answers 2

9

You can use Array.prototype.map:

var newArray = oldArray.map(function (obj) {
    return obj.email;
});
Sign up to request clarification or add additional context in comments.

3 Comments

I think this would be the answer. However, node.js is throwing. TypeError: Object #<RowDataPacket> has no method 'map'. Anyway, that's a different problem that I need to solve.
Would it be correct, if I add the .map answer inside the forEach loop?
See if you can do something like Array.prototype.splice.call(this, rows).map(...);, I don't know what RowDataPacket is but maybe a slice works. Otherwise you have to loop with foreach and store the values in a new array.
2

here is your solution

var rows =[
    { email: '[email protected]' },
    { email: '[email protected]' }
];
var yop = [];
rows.forEach(function(entry) {
    yop.push(entry.email);
});

console.log(yop);

1 Comment

This would work although there is no need for the new keyword.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.