2016-07-27 105 views
0

我有下面這個對象數組。循環遍歷一組對象並獲得只有幾個對象

[Object, Object, Object] 
    0:Object 
     name: "Rick" 
     Contact: "Yes" 
     id: 1 
    1:Object 
     name:"Anjie" 
     Contact:"No" 
     id: 2 
    2:Object 
     name:"dillan" 
     Contact:"Maybe" 
     id:3 

現在,我只想要它的名稱和聯繫人對象。我怎麼能得到這個。另外,在其他情況下,我想要名稱和ID。請有人讓我知道如何實現這一點。

例如,只有名字和聯繫人應該給出這個結果。

[Object, Object, Object] 
    0:Object 
     name: "Rick" 
     Contact: "Yes" 
    1:Object 
     name:"Anjie" 
     Contact:"No" 
    2:Object 
     name:"dillan" 
     Contact:"Maybe" 
+1

什麼你不知道該怎麼辦?看起來像一個非常簡單的問題。你卡在哪裏? – 2016-07-27 19:38:46

+1

[此前一個問題](http://stackoverflow.com/questions/38248004/convert-array-of-objects-and-their-properties-to-array)你的很多東西都可以在這裏找到。 – 2016-07-27 19:40:00

+0

name和contact是Object的屬性。它們是絃樂,當然它們是物體。但將它們描述爲屬性會更清楚。無論如何,你可以使用Array.map來實現這一點 –

回答

1

最簡單的方法是使用map功能:

var objs = objs.map(function(obj) { 
    return { 
     name: obj.name, 
     Contact: obj.Contact 
    } 
}); 

或者,您可以遍歷它手動:

var objs = [{ 
 
     name: "Rick", 
 
     Contact: "Yes", 
 
     id: 1 
 
}, { 
 
     name:"Anjie", 
 
     Contact:"No", 
 
     id: 2 
 
}, { 
 
     name:"dillan", 
 
     Contact:"Maybe", 
 
     id:3 
 
}]; 
 

 
var newObjs = []; 
 

 
for (var i=0, len = objs.length; i < len; i++) 
 
{ 
 
    newObjs.push({ 
 
     name: objs[i].name, 
 
     Contact: objs[i].Contact 
 
    }); 
 
} 
 

 
console.log(newObjs);

1

如果你想訪問數組中對象的一些屬性不是簡單的,你可以使用一個for循環和傳遞價值的我喜歡: -

for(var i=0;i<object.length;i++){ 
    object[i].name // give you name 
    object[i].contact // give you contact do whatever you want to do with this 
} 

或者乾脆寫一個函數,使一個單獨的對象從數組的對象。

1

你可以使用下劃線的mappick

var result = _.map(data, item => _.pick(item, 'name', 'Contact')); 
0

你可以使用map()和返回與已刪除屬性的新對象的數組。要創建對象的副本,你可以使用(JSON.parse(JSON.stringify()))

var data = [{ id: 1, name: "Rick", Contact: "Yes" }, { id: 2, name: "Anjie", Contact: "No" }, { id: 3, name: "dillan", Contact: 'Maybe' }]; 
 

 
var result = data.map(function(e) { 
 
    var r = (JSON.parse(JSON.stringify(e))); 
 
    delete r.id; 
 
    return r; 
 
}); 
 

 
console.log(result) 
 
console.log(data)

或者你的使用Object.assign({}, e)

var data = [{ id: 1, name: "Rick", Contact: "Yes" }, { id: 2, name: "Anjie", Contact: "No" }, { id: 3, name: "dillan", Contact: 'Maybe' }]; 
 

 
var result = data.map(function(e) { 
 
    var r = Object.assign({}, e); 
 
    delete r.id; 
 
    return r; 
 
}); 
 

 
console.log(result); 
 
console.log(data);

1

一個通用的解決方案coould是一個給定的數據數組和鍵陣列。所需的屬性映射到一個新的數組中。

function getWithKeys(array, keys) { 
 
    return array.map(function (a) { 
 
     var o = {}; 
 
     keys.forEach(function (k) { 
 
      o[k] = a[k]; 
 
     }); 
 
     return o; 
 
    }); 
 
} 
 

 
var array1 = [{ name: "Rick", Contact: "Yes", id: 1 }, { name: "Anjie", Contact: "No", id: 2 }, { name: "dillan", Contact: "Maybe", id: 3 }], 
 
    array2 = getWithKeys(array1, ['name', 'Contact']), 
 
    array3 = getWithKeys(array1, ['name', 'id']); 
 

 
console.log(array2); 
 
console.log(array3);