2016-08-30 164 views
0

我是新來編碼的。請親切回答:)從對象數組中提取值

我有一個對象數組。

[ { UPDATED_BY: '2301132411' }, 
    { UPDATED_BY: '1432007924' }, 
    { UPDATED_BY: '973551993' }, 
    { UPDATED_BY: '1082191138' }, 
    { UPDATED_BY: '1079399273' } ] 

我想在下面格式的輸出: ( '2301132411', '1432007924', '973551993', '1082191138', '1079399273')

我能夠輸出一個值通過使用下面的命令。我想在這裏使用循環來獲取所有values.Please幫助確切code.Thanks提前。

var k={}; 
var arr = [ { UPDATED_BY: '2301132411' }, 
    { UPDATED_BY: '1432007924' }, 
    { UPDATED_BY: '973551993' }, 
    { UPDATED_BY: '1082191138' }, 
    { UPDATED_BY: '1079399273' } ]; 
    k=arr[0]; 
console.log(k); 

output: 1432007924 

回答

0

試試這個:

var output = []; 
for(var i = 0;i < arr.length; i++) { 
    output.push(arr[i]['UPDATED_BY']); 
} 
console.log(output); 
1

如果我有你的問題理解正確的話,你可以用Array#map函數來實現這一目標如下:

var arr = [ 
 
    { UPDATED_BY: '2301132411' }, 
 
    { UPDATED_BY: '1432007924' }, 
 
    { UPDATED_BY: '973551993' }, 
 
    { UPDATED_BY: '1082191138' }, 
 
    { UPDATED_BY: '1079399273' } 
 
]; 
 
var k = arr.map(function(obj) { 
 
    return obj['UPDATED_BY']; 
 
}); 
 

 
console.log(k); // If you wants an array of all values 
 
console.log('(' + k + ')'); // In case you wants one complete string

1

var arr = [ 
 
    {UPDATED_BY: '2301132411'}, 
 
    {UPDATED_BY: '1432007924'}, 
 
    {UPDATED_BY: '973551993'}, 
 
    {UPDATED_BY: '1082191138'}, 
 
    {UPDATED_BY: '1079399273' 
 
}] 
 

 
arr = '(' + arr.map(function (item) { 
 
    return item.UPDATED_BY 
 
}) + ')'; 
 

 
console.log(arr);