Reputation: 1837
I have array of JSON objects like this:
[ { id: 841,
when: 'date',
who: 'asd',
what: 'what',
key1 : 'a key',
key2 : 'a key'
_id: 544034ab914ae3b9270545c1,
__v: 0 },
{ id: 841,
when: 'date',
who: 'asd',
what: 'what',
key1 : 'a key',
key2 : 'a key'
_id: 544034ab914ae3b9270545c1,
__v: 0 } ]
I want to cut key1
and key2
from that objects and want to see this:
[ { id: 841,
when: 'date',
who: 'asd',
what: 'what',
_id: 544034ab914ae3b9270545c1,
__v: 0 },
{ id: 841,
when: 'date',
who: 'asd',
what: 'what',
_id: 544034ab914ae3b9270545c1,
__v: 0 } ]
How can I cut that keys and values?
My method is not working. (Pseudo) :
var new_array
for i old_array.length
delete old_array[i].key1
delete old_array[i].key2
new_array.push(old_array[i])
Upvotes: 0
Views: 1469
Reputation: 1837
I used underscore.js
var cutted_object = _.omit(json_object, "key1", "key2"
"key2", "date",
"id33", "etc");
Upvotes: 0
Reputation: 71384
You pseudo-code is basically what you need, though you can just delete the properties in place:
var arr = [...]; // your array
for (var i = 0; i < arr.length; i++) {
delete arr[i].key1;
delete arr[i].key2;
}
Upvotes: 0
Reputation: 10627
Like this:
function deleteProps(){
var a = Array.slice(arguments) || Array.prototype.slice.call(arguments);
var oa = a[0], dp = a.slice(1);
for(var i=0,l=oa.length; i<l; i++){
for(var n=0,c=dp.length; n<c; n++){
delete oa[i][dp[n]];
}
}
return oa;
}
var newObjectsArray = deleteProps(objectsArray, 'key1', 'key2');
Upvotes: 0
Reputation: 1368
yourArray = yourArray.map(function(current, index, arr){
delete current.key1;
delete current.key2;
return current;
});
That should do what you wish ;-)
Hope that helps, Jan
Upvotes: 1