Say I have an array of objects. There are certain keys/values I don't want. The traditional way to delete one key/value pair would be to use delete
as so:
for (var i = 0; i < tracks.length; i++) {
delete tracks[i]["currency"];
...
}
The objects I'm pulling in could have over 30 pairs. Is there a way where I can state which pairs I want and remove all others? So for example in this array of objects I only want to keep 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
}];
Say I have an array of objects. There are certain keys/values I don't want. The traditional way to delete one key/value pair would be to use delete
as so:
for (var i = 0; i < tracks.length; i++) {
delete tracks[i]["currency"];
...
}
The objects I'm pulling in could have over 30 pairs. Is there a way where I can state which pairs I want and remove all others? So for example in this array of objects I only want to keep 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
}];
Share
Improve this question
asked Apr 14, 2015 at 18:12
Carl EdwardsCarl Edwards
14.5k12 gold badges66 silver badges131 bronze badges
3
- Why not just loop and delete? – thefourtheye Commented Apr 14, 2015 at 18:15
- you will need to make a function to do it for you, which would (depending) return a new object – Fallenreaper Commented Apr 14, 2015 at 18:16
-
why don't you use
.map()
which iterates over the array. Return the values you want as a object and replace the variable tracks – Dhiraj Commented Apr 14, 2015 at 18:17
6 Answers
Reset to default 4Itrate over the array and keep what you want from each 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];
}
}
This is what I would suggest as I feel it is more elegant:
// 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;
do you mind use underscore ".pick(object, *keys) " , ".omit(object, *keys) " and push the new objects into a new array.
A self-descriptive solution:
// 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];
The simplest solution that I can e with:
/**
* 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))
Note: avoid using of delete
, it changes "shape" of object and harms performance. Use item.prop = null
where possible. My function returns new collection, so it is not necessary, but if you plan to populate the new collection again, it would be better to set unwanted props to null
.
Not sure if this is what you want, but it will loop over list, and return the same list with only the given keys in it. keys is an Array of 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;
}
you can also write this in a few other ways to modify the original list, but i am unsure if that was particularly what you wanted, so i just returned a new list.
if you want to change the original, you could do something like:
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));
I believe that should also do what is needed. it will loop over list, and remove all keys not in the select.