说我有一个对象数组。我不想要某些键/值。删除一个键/值对的传统方法是使用delete
:
for (var i = 0; i < tracks.length; i++) {
delete tracks[i]["currency"];
...
}
我拉的物体可能有30多对。有没有办法可以说明我想要哪些对并删除所有其他对?例如,在这个对象数组中,我只想保留trackName
,kind
,price
var tracks = [{
trackNumber: "01",
trackName: "Track 1",
trackDuration: "5:35",
kind: "song",
currency: "USD",
price: 1.29
}, {
trackNumber: "02",
trackName: "Track 2",
trackDuration: "5:15",
kind: "song",
currency: "USD",
price: 1.29
}, {
trackNumber: "03",
trackName: "Track 3",
trackDuration: "5:07",
kind: "song",
currency: "USD",
price: 1.29
}, {
trackNumber: "04",
trackName: "Track 4",
trackDuration: "0:16",
kind: "song",
currency: "USD",
price: 1.29
}, {
trackNumber: "05",
trackName: "Track 5",
trackDuration: "5:35",
kind: "song",
currency: "USD",
price: 1.29
}];
答案 0 :(得分:4)
迭代数组并保留每个Object的所需内容。
var keep = ['trackName', 'kind', 'price'];
for(var i = 0;i < tracks.length; i++){
for(var key in tracks[i]){
if(keep.indexOf(key) === -1)delete tracks[i][key];
}
}
答案 1 :(得分:1)
这就是我的建议,因为我觉得它更优雅:
// create new array of objects from old one
var fixed = jQuery.map(tracks, function(element, index){
return {"trackName": element.trackName, "kind": element.kind, "price": element.price};
});
// delete old array
tracks = null;
答案 2 :(得分:0)
你介意使用下划线&#34; .pick(object,* keys)&#34; ,&#34; .omit(对象,*键)&#34;并将新对象推送到新数组中。
答案 3 :(得分:0)
自我描述性解决方案:
// what you want to keep:
var want_to_keep = ['trackName', 'kind', 'price'];
// loop trough every main element of tracks:
for (var i=0; i<tracks.length; i++)
// now go trough every element of the current track:
for (var key in tracks[i])
// check if the current track key is wanted:
if (want_to_keep.indexOf(key) < 0)
delete tracks[i][el];
答案 4 :(得分:0)
我可以提供最简单的解决方案:
/**
* Returns new collection which items contains only specified properties
* @param {Array} collection of items
* @param {Array} properties to keep
*/
function keepCollectionProperties(collection, properties) {
return collection.map(function(item) {
var newItem = {}
for(var i = 0, prop; prop = properties[i]; i++)
if (typeof item[prop] !== 'undefined')
newItem[prop] = item[prop]
return newItem
})
}
var tracks = [{
trackNumber: "01",
trackName: "Track 1",
trackDuration: "5:35",
kind: "song",
currency: "USD",
price: 1.29
}, {
trackNumber: "02",
trackName: "Track 2",
trackDuration: "5:15",
kind: "song",
currency: "USD",
price: 1.29
}, {
trackNumber: "03",
trackName: "Track 3",
trackDuration: "5:07",
kind: "song",
currency: "USD",
price: 1.29
}, {
trackNumber: "04",
trackName: "Track 4",
trackDuration: "0:16",
kind: "song",
currency: "USD",
price: 1.29
}, {
trackNumber: "05",
trackName: "Track 5",
trackDuration: "5:35",
kind: "song",
currency: "USD",
price: 1.29
}];
var output = keepCollectionProperties(tracks, ['trackName', 'kind', 'price'])
document.write(JSON.stringify(output))
注意:避免使用delete
,它会改变对象的“形状”并损害性能。尽可能使用item.prop = null
。我的函数返回新集合,因此没有必要,但如果您打算再次填充新集合,最好将不需要的道具设置为null
。
答案 5 :(得分:-1)
不确定这是否是您想要的,但它会循环遍历列表,并返回相同的列表,其中只包含给定的键。 keys是一个键数组。
function LimitArray(keys, list){
var returnArr = [];
for (var i in list){
var row = {};
for (key in list[i]){
row[key] = list[i][key];
}
returnArr.push(row);
}
return returArr;
}
您也可以通过其他几种方式来修改原始列表,但我不确定这是否是您想要的,所以我刚刚返回了一个新列表。
如果您想更改原件,可以执行以下操作:
var select = ["a","b"]
list.map(function(val,index, array)
{
var keys = Object.keys(val);
for (var key in keys){
if(-1 == select.indexOf(keys[key]]) delete val[keys[key]];
}}.bind(this));
我相信也应该做所需的事情。它将循环遍历列表,并删除不在选择中的所有键。