简体   繁体   中英

immutablejs, merge (or extend) immutable object

I have a function where I am trying to update an immuteable object, I'm trying to get an extend type of functionality, so if the item exists it is not duplicated. So far, I have tried this :

var toggleFilter = function(parent, child){
    let newActive = {};
    newActive[parent] = child;
    return state.merge('activeFilters', newActive);
};

This does not seem to be working correctly. The active filters on that state (which is an immutable object) just looks kind of like this

{ 
  "parent1" : [..array of children],
  "parent2" : [...array of children]
}

And those strings coming in look like Parent3 , child2 , so I would want them just to be added in like :

 { 
  "parent1" : [..array of children],
  "parent2" : [...array of children],
  "parent3" : ["child2"]
}

But it could also be parent1, child1 - so I would like that to get put into the parent1 key, kind of like the extend functionality. Would appreciate any help, thanks!

var toggleFilter = function(parent, child){
    // we create a new children array 
    let children = state.get('activeFilters.' + parent) || new List();
    // we check for child's pre-existence
    if (children.indexOf(child) > -1)
      return state;
    children = children.push(child);
    // we add it back to state
    return state.set('activeFilters.'+parent, children);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM