2013-07-19 52 views
0

我有以下javascript數組var rows在Javascript上,如何重新排列數組

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

其中,我想讓它只是......

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

所以,我想它循環使用的forEach

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

不過,我卡住了。

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

任何幫助表示讚賞。

+0

注意:您已經有標記'node.js'和'javascript'的問題,所以他們不需要在標題中再次列出。 –

+0

好的。注意到喬納森。 –

回答

9

您可以使用Array.prototype.map

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

我認爲這將是答案。但是,node.js正在拋出。 TypeError:Object#沒有方法'map'。無論如何,這是我需要解決的一個不同的問題。 –

+0

如果我在forEach循環中添加.map答案,它會是正確的嗎? –

+0

看看你是否可以做一些像'Array.prototype.splice.call(this,rows).map(...);',我不知道'RowDataPacket'是什麼,但也許是一個切片的作品。否則,你必須循環foreach並將值存儲在一個新的數組中。 –

2

這裏是您的解決方案

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

console.log(yop); 
+0

雖然沒有必要使用'new'關鍵字, –