Louie Miranda
Louie Miranda

Reputation: 1159

On Javascript, how to re-arrange an array

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.

Upvotes: 0

Views: 498

Answers (2)

GwenM
GwenM

Reputation: 1335

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

Upvotes: 2

James Allardice
James Allardice

Reputation: 166031

You can use Array.prototype.map:

var newArray = oldArray.map(function (obj) {
    return obj.email;
});

Upvotes: 9

Related Questions