3

I have some data that I want to iterate through and reject those with a disabled: true property.

however when I try _.reject my object gets turned into an array.

var data = {
  stuff: {
     item1: {
        name: "one",
        disabled: true
     },
     item2: {
        name: "two"
     }

  }
};


data.stuff = _.reject(data.stuff, function(val){
    return val.disabled;
});

data.stuff is now an array, rather than an Object. I've lost all my keys.

2
  • 1
    _.reject can only return an array.
    – user229044
    Jan 5, 2014 at 7:50
  • 1
    Semantically, your stuff actually even should be an array. Really. item1, item2 either is useful data (then turn it into an id property, respectively) or it isn't (then drop it).
    – Tomalak
    Jan 5, 2014 at 8:29

7 Answers 7

5

You could use _.omit() instead.

1
  • doesn't work - omit returns the same data, even when sent data.stuff
    – gdibble
    Feb 4, 2016 at 21:01
4

What @archie said but in code:

data.stuff = _.reduce(data.stuff, function(memo, value, key){
    if( !value.disabled) memo[key] = value;
    return memo;
}, {});
2
  • this is the only solution in this question that will work inline (returning data) and actually returns the correct answer ::: { item2:{ name:"two" } }
    – gdibble
    Feb 4, 2016 at 21:06
  • 2
    In fact, omit() is better, because it's a tool intended precisely for the job at hand.
    – tybro0103
    Feb 5, 2016 at 22:33
3

When you pass data.stuff (which is an Object) to _.reject, it picks only the values of the object and passes that to _.reject. So, key information is lost and reconstructing the object is not possible.

Instead, you can do it like this

data.stuff = _.object(_.filter(_.pairs(data.stuff), function(list) {
    return list[1].disabled;
}));

console.log(data);

Output

{ stuff: { item1: { name: 'one', disabled: true } } }

How it works

  1. _.pairs converts the {key:value} pairs into [key, value] array.

  2. _.filter filters the items whose disabled is a falsy value.

  3. _.object converts the filtered [key, value] array into {key:value} pairs.

2
  • 1
    While this works, there's a much simpler solution using omit(). See below.
    – tybro0103
    Sep 3, 2015 at 21:57
  • this solution is tailored to underscore but _.pairs is not a fn in lodash
    – gdibble
    Feb 4, 2016 at 21:09
1

I ended up just using native javascript instead of underscore:

//remove disabled items
for ( var item in data.stuff ) {
    if ( data.stuff[item].disabled ) {
        delete data.stuff[item];
    }
}
1
  • i like this Vanilla JS solution, but it would have been best if it worked inline like you were originally trying. for the only solution herein that works, try @Gruff Bunny's answer +1
    – gdibble
    Feb 4, 2016 at 21:04
1

Use _.omit():

_.omit(data.stuff, function(val) { return val.disabled; });

See fiddle: https://jsfiddle.net/ehbmsb5k/

2
  • does not work - omit returns the same data, even passed sent data.stuff
    – gdibble
    Feb 4, 2016 at 21:02
  • @gdibble It does indeed work. I updated answer with a fiddle.
    – tybro0103
    Feb 5, 2016 at 22:30
0

Have you looked at doing this with _reduce, where the memo is a new hash, in which you merge the items( key, value pair) you need.

-1

The original data.stuff is not changed when you use reject. But since you are assigning to the stuff key, the original data is lost.

_.reject is supposed to work on and return arrays. You could try this in order to accomplish what you want:

_.each(Object.keys(data.stuff), function(val) {
    if (data.stuff[val].disabled)
        delete data.stuff[val];
});
1
  • ): returned ["item1", "item2"]
    – gdibble
    Feb 4, 2016 at 21:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.