简体   繁体   中英

Remove array value from object using underscore

I have following object

object = {
  "key1": "value1",
  "key2": "value2",
  "key3": [
     "arrayKey1": "arrayValue1",
     "arrayKey2": "arrayValue2",
     "arrayKey3": "arrayValue3"
   ]
};

When i am using _.omit(object, key3) is working fine. As it is removing the 'key3' correctly. but what if i want to remove the arrayKey2 only and not the whole key3 ?

If object.key3 is actually an array:

object.key3 = _.reject(object.key3, function(val, idx){ return idx == 1 })
// Or using the new ES6 syntax:
object.key3 = _.reject(object.key3, (val, idx) => idx == 1)

If object.key3 is an object:

object.key3 = _.omit(object.key3, 'arrayKey2')

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