Emphasize: remove all key / value pairs from the object array

Is there a smart way of emphasizing the removal of all key / value pairs from an array of an object?

eg. I have the following array:

var arr = [ { q: "Lorem ipsum dolor sit.", c: false }, { q: "Provident perferendis veniam similique!", c: false }, { q: "Assumenda, commodi blanditiis deserunt?", c: true }, { q: "Iusto, dolores ea iste.", c: false }, ]; 

and I want to get the following:

 var newArr = [ { q: "Lorem ipsum dolor sit." }, { q: "Provident perferendis veniam similique!" }, { q: "Assumenda, commodi blanditiis deserunt?" }, { q: "Iusto, dolores ea iste." }, ]; 

I can get this working with JS below, but not very happy with my solutions:

 for (var i = 0; i < arr.length; i++) { delete arr[i].c; }; 

Any suggestions that are very much appreciated.

+27
javascript arrays
source share
2 answers

You can use map and omit to exclude certain properties, e.g.

 var newArr = _.map(arr, function(o) { return _.omit(o, 'c'); }); 

Or map and pick include only certain properties, for example:

 var newArr = _.map(arr, function(o) { return _.pick(o, 'q'); }); 
+47
source share

For Omit

 _.map(arr, _.partial(_.omit, _, 'c')); 

To select

 _.map(arr, _.partial(_.pick, _, 'q')); 
0
source share

All Articles