Set all Object keys to false

后端 未结 9 1486
执念已碎
执念已碎 2021-01-03 19:50

Lets say I have an object

  filter: {
    \"ID\": false,
    \"Name\": true,
    \"Role\": false,
    \"Sector\": true,
    \"Code\": false
  }
9条回答
  •  北海茫月
    2021-01-03 20:20

    Using lodash, mapValues is a graceful, loop-free way:

    filter = {
        "ID": false,
        "Name": true,
        "Role": false,
        "Sector": true,
        "Code": false
    };
    
    filter = _.mapValues(filter, () => false);
    

    If you want to do this with Underscore.js, there is an equivalent, but with a slightly different name:

    filter = _.mapObject(filter, () => false);
    

    In either case, the value of filter will be set to:

    { ID: false, 
      Name: false, 
      Role: false, 
      Sector: false, 
      Code: false }
    

提交回复
热议问题