admin管理员组

文章数量:1406052

I am trying to determine whether or not a certain part of my immutable map contains a certain key value, checked: true, and if so, set checked: true on the upper level. The object I am looking over looks like so:

const memo = {
  "Topics": {
    "filters": {
      "Psychological disorders": {
        "filters": {
          "Anxiety disorders": {
            "filters": {},
            "checked": true <-- check for this value at this level
          }
        }
      }
    },
    "isOpen": false
  }
}

Note: this data is actually immutable.

So after the function ran it would change into this :

const memo = {
      "Topics": {
        "filters": {
          "Psychological disorders": {
            "checked": true, <-- check true added
            "filters": {
              "Anxiety disorders": {
                "filters": {},
                "checked": true 
              }
            }
          }
        },
        "isOpen": false
      }
    }

The only var I have access to right now is the "Topics" name, so what I am trying to find out if is any Topics -> filters -> filters has checked: true inside it. Initially, i was trying to just toJS() this memo and check inside with lodash.

Something like this :

_.find(memo['Topics'], _.flow(
    _.property('filters'),
    _.property('filters'),
    _.partialRight(_.any, { checked: true })
));

I can use this information to modify the object, but I am wondering if it is possible to achieve this without taking it out of immutable. So far I have tried this :

   const nameCheck = 'Topics';

   const hasCheckedFilters = memo.update(nameCheck, Map(),
        (oldResult) => oldResult.update('filters', Map(),   
            (oldSection) => {
                // filters inside this object has checked : true, .set('checked', true);
                const fromImmutable = oldSection.toJS();
                const checkForChecked = _.find(fromImmutable.filters, {checked:true});
               if(checkForChecked) {
                   oldSectsion.set('checked', true);
               }

              }
            )
        )
    );

This does not seem to work because i am not looping over the filters. Would appreciate any input, Thanks!

I am trying to determine whether or not a certain part of my immutable map contains a certain key value, checked: true, and if so, set checked: true on the upper level. The object I am looking over looks like so:

const memo = {
  "Topics": {
    "filters": {
      "Psychological disorders": {
        "filters": {
          "Anxiety disorders": {
            "filters": {},
            "checked": true <-- check for this value at this level
          }
        }
      }
    },
    "isOpen": false
  }
}

Note: this data is actually immutable.

So after the function ran it would change into this :

const memo = {
      "Topics": {
        "filters": {
          "Psychological disorders": {
            "checked": true, <-- check true added
            "filters": {
              "Anxiety disorders": {
                "filters": {},
                "checked": true 
              }
            }
          }
        },
        "isOpen": false
      }
    }

The only var I have access to right now is the "Topics" name, so what I am trying to find out if is any Topics -> filters -> filters has checked: true inside it. Initially, i was trying to just toJS() this memo and check inside with lodash.

Something like this :

_.find(memo['Topics'], _.flow(
    _.property('filters'),
    _.property('filters'),
    _.partialRight(_.any, { checked: true })
));

I can use this information to modify the object, but I am wondering if it is possible to achieve this without taking it out of immutable. So far I have tried this :

   const nameCheck = 'Topics';

   const hasCheckedFilters = memo.update(nameCheck, Map(),
        (oldResult) => oldResult.update('filters', Map(),   
            (oldSection) => {
                // filters inside this object has checked : true, .set('checked', true);
                const fromImmutable = oldSection.toJS();
                const checkForChecked = _.find(fromImmutable.filters, {checked:true});
               if(checkForChecked) {
                   oldSectsion.set('checked', true);
               }

              }
            )
        )
    );

This does not seem to work because i am not looping over the filters. Would appreciate any input, Thanks!

Share Improve this question edited Apr 19, 2016 at 18:47 ajmajmajma asked Apr 19, 2016 at 18:39 ajmajmajmaajmajmajma 14.2k25 gold badges83 silver badges138 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

This keeps the data immutable. I am looping through each property in Topics.filters, then checking if that property has a filters key. If it does loop through that. If checked is true in the subitem then set checked to true in the parent item and return a new map.

const memo = {
      "Topics": {
        "filters": {
          "Psychological disorders": {
            "checked": false,
            "filters": {
              "Anxiety disorders": {
                "filters": {},
                "checked": true 
              }
            }
          }
        },
        "isOpen": false
      }
    };

let map = Immutable.fromJS(memo);

map.getIn(['Topics','filters']).forEach((item, i) => {
  if(item.has('filters')){
    item.get('filters').forEach(subItem => {
      if(subItem.get('checked') === true){
        map = map.setIn(['Topics', 'filters', i], item.set('checked', true)); 
      }
    });  
  }
});

console.log(JSON.stringify(map.toJS()));

http://jsbin./gorohewuji/edit?js,console

本文标签: javascriptimmutablejsfind if map contains key valueStack Overflow