0

I have an array of objects that might look like this:

var arr = [{
  a: 1,
  b: 321,
  c: 556,
  d: 8
}, {
  a: 1,
  b: 22,
  c: 21,
  d: 8
}, {
  a: 1,
  b: 1,
  c: 43,
  d: 8
}, ];

and another list that could be:

var list = ['a', 'c', 'd'];

Since my list only has keys a, c, and d I want to get rid of all instances of the b key on my original array. All this procedure has to be dynamic though because There is no way for me to know what those keys might be prior to receiving them.

Is there a nice and clean way of doing this in JavaScript?

4
  • Did you write any code? Commented Oct 30, 2015 at 14:44
  • You want to merge? Something like list = [1, 1, 1, 556, 21, 43, 8, 8, 8] ? Commented Oct 30, 2015 at 14:46
  • @ergonaut there is no need to post my code in this sort of question. I do not have an issue in doing it, so that someone then has to fix my code. I am more interested on if there is some js trick I am not aware of. If you would have seen my code then it would be identical to Siguza's minus James's suggestion. Commented Oct 30, 2015 at 14:52
  • judging from your comments, your definition of "clean" is that you want a js function that isn't in library that somehow does not involve loops. Then the answer is NO. Commented Oct 30, 2015 at 14:56

3 Answers 3

1
arr.forEach(function(element)
{
    for(var key in element)
    {
        if(list.indexOf(key) === -1)
        {
            delete element[key];
        }
    }
});

Should be pretty self-explanatory.

If you don't want to modify the original array:

arr.map(function(element)
{
    element = JSON.parse(JSON.stringify(element));
    for(var key in element)
    {

        if(list.indexOf(key) === -1)
        {
            delete element[key];
        }
    }
    return element;
});
Sign up to request clarification or add additional context in comments.

3 Comments

You could shorten this a bit by using arr.forEach since you're modifying the objects in place. No need to return anything then.
@JamesAllardice Thanks for the hint, updated my answer.
Even though underscore is not a bad alternative I will go with this answer. I think it would be of more interest to someone visiting the thread in the future.
1

Consider using the underscore.js library. It contains a reject method that should do the trick

reject _.reject(list, predicate, [context]) Returns the values in list without the elements that the truth test (predicate) passes. The opposite of filter.

var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });

=> [1, 3, 5]

2 Comments

_.reject is awesome!
I sort of have a feeling that i the end I will go with underscore, but I hoped to not have to depend to it.
0

Is that what you're looking for?

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

// Update, a vanilla approach

var reject = [ "b" ];
for(var i=0, i < arr.length; i++) {
  for(var j in arr[i]) {
    if(reject.indexOf(j) != -1) delete arr[i][j];
  }
}

1 Comment

This cannot be it because it has to be dynamic. If we make your solution dynamic then it becomes similar to Siguza's.

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.