简体   繁体   中英

How to remove properties from object using lodash?

This is my object:

var obj = {
  "flag": false,
  "text": "good text",
  "someArray": [
    {
      "questionId": 11,
      "text": "qwe",
      "TypeId": "B",
      "source": "oooooo"
    },
    {
      "questionId": 12,
      "text": "zxc",
      "TypeId": "A",
      "source": "pppppp"
    },
    {
      "questionId": 13,
      "text": "asd",
      "TypeId": "D",
      "source": "cccccc"
    }
  ]
}

What I'm trying to do is remove from objects in someArray properties TypeId and source . How can I do this with lodash? I was trying using _.pick, but I don't know how can I use this inside someArray .

You want to use the opposite of _.pick , which is _.omit and return the objects without the given properties by mapping them with _.map

 var obj = { "flag": false, "text": "good text", "someArray": [{ "questionId": 11, "text": "qwe", "TypeId": "B", "source": "oooooo" }, { "questionId": 12, "text": "zxc", "TypeId": "A", "source": "pppppp" }, { "questionId": 13, "text": "asd", "TypeId": "D", "source": "cccccc" }] } obj.someArray = _.map(obj.someArray, o => _.omit(o, ['TypeId', 'source'])); console.log(obj) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> 

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