javascript - Dynamically removing multiple key/value pairs from an object -


say have array of objects. there keys/values don't want. traditional way delete one key/value pair use delete so:

for (var = 0; < tracks.length; i++) {   delete tracks[i]["currency"];   ... } 

the objects i'm pulling in have on 30 pairs. there way can state pairs want , remove others? example in array of objects want 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 }]; 

itrate on array , keep want each object.

var keep = ['trackname', 'kind', 'price'];  for(var = 0;i < tracks.length; i++){      for(var key in tracks[i]){         if(keep.indexof(key) === -1)delete tracks[i][key];     }  } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -