最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - using _.reject turns my object into an array - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Feb 5, 2016 at 22:31 tybro0103 49.7k34 gold badges148 silver badges172 bronze badges asked Jan 5, 2014 at 7:30 chovychovy 75.8k62 gold badges245 silver badges319 bronze badges 2
  • 1 _.reject can only return an array. – user229044 Commented 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 Commented Jan 5, 2014 at 8:29
Add a ment  | 

7 Answers 7

Reset to default 5

You could use _.omit() instead.

What @archie said but in code:

data.stuff = _.reduce(data.stuff, function(memo, value, key){
    if( !value.disabled) memo[key] = value;
    return memo;
}, {});

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.

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

Use _.omit():

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

See fiddle: https://jsfiddle/ehbmsb5k/

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.

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 acplish what you want:

_.each(Object.keys(data.stuff), function(val) {
    if (data.stuff[val].disabled)
        delete data.stuff[val];
});
发布评论

评论列表(0)

  1. 暂无评论