从对象中动态删除多个 key/value 对
Dynamically removing multiple key/value pairs from an object
假设我有一组对象。有些keys/values我不想要。删除 one key/value 对的传统方法是这样使用 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
}];
您介意使用下划线“.pick(object, *keys) ”、“.omit(object, *keys) ”并将新对象推入新数组吗.
不确定这是否是您想要的,但它会遍历列表,并且 return 相同的列表,其中只有给定的键。 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;
}
您也可以用其他几种方式编写此代码以修改原始列表,但我不确定这是否正是您想要的,所以我只是 return 编辑了一个新列表。
如果你想改变原来的,你可以这样做:
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));
我相信应该也是该做的事。它将遍历列表,并删除所有不在 select.
中的键
迭代数组并保留每个对象的所需内容。
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];
}
}
一个自我描述的解决方案:
// 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];
这是我的建议,因为我觉得它更优雅:
// 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;
我能想到的最简单的解决方案:
/**
* 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
,它会改变"shape"对象并损害性能。尽可能使用 item.prop = null
。我的函数 returns new collection,所以没有必要,但是如果你打算再次填充新的 collection,最好将不需要的 props 设置为 null
.
假设我有一组对象。有些keys/values我不想要。删除 one key/value 对的传统方法是这样使用 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
}];
您介意使用下划线“.pick(object, *keys) ”、“.omit(object, *keys) ”并将新对象推入新数组吗.
不确定这是否是您想要的,但它会遍历列表,并且 return 相同的列表,其中只有给定的键。 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;
}
您也可以用其他几种方式编写此代码以修改原始列表,但我不确定这是否正是您想要的,所以我只是 return 编辑了一个新列表。
如果你想改变原来的,你可以这样做:
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));
我相信应该也是该做的事。它将遍历列表,并删除所有不在 select.
中的键迭代数组并保留每个对象的所需内容。
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];
}
}
一个自我描述的解决方案:
// 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];
这是我的建议,因为我觉得它更优雅:
// 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;
我能想到的最简单的解决方案:
/**
* 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
,它会改变"shape"对象并损害性能。尽可能使用 item.prop = null
。我的函数 returns new collection,所以没有必要,但是如果你打算再次填充新的 collection,最好将不需要的 props 设置为 null
.