0

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 key2from 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])
2
  • 1
    What exactly isn't working? This should be fine. But it's worth noting that you shouldn't need to create a new array. Delete edits the object in place, so if you delete the property from the object contained in the array, the original array should reflect those changes immediately Commented Oct 16, 2014 at 21:33
  • @Brennan I don't know but it's pushing old version of objects. By the way those are from a mongodb query, if it's matter. Commented Oct 16, 2014 at 22:04

4 Answers 4

1
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

Sign up to request clarification or add additional context in comments.

8 Comments

More memory intensive than just deleting in place.
Thats true, thanks for pointing it out, Mike! You could do "delete arr[index].key1;" and same for key2.
Thanks. I'm getting [ undefined, undefined ] with both of your methods. What can be caused to this?
Are you still reassigning the array like yourArray = yourArray.map(..)? It could be that this is the problem, in the memory-friendly version you just do yourArray.map(<in-place-deletion-method>)
OH NEVERMIND, you need to return current; after deleting the keys in my code above! I'll edit.
|
0

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');

1 Comment

TypeError: Object #<Object> has no method 'slice'. Thanks but there is a problem, it can't splice
0

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;
}

6 Comments

This method wasn't working and I couldn't find why. Thank you. @Mike Brant
Lets nitpick a bit, should be var i = 0 ;-)
... and array is undefined.
@Lazy Fixed my dumb variable naming problem :) This should work for you now.
Thank you. I solved this problem using _.omit feature of underscore.js.
|
0

I used underscore.js

var cutted_object =  _.omit(json_object, "key1", "key2" 
                                         "key2", "date",
                                         "id33", "etc");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.