简体   繁体   中英

In lodash how can I deep remove?

I have a data structure as such at the bottom.. and I need to remove from the array the totype.id === 'REG' and also all instances of the object property budgt .

I have accomplished part of this using the following code, and was going to do another iteration to remove the budgt from all remaining totype s but that didn't seem the best way to do it.

_.each(data, function (d, i) { // iterate over each "org"
  _.each(d.occs, function (occ) { // iterate over each "occ" in current "org"
    return _.remove(occ.totypes, function (totype) { // remove the totype REG from "totypes"
      return totype.id === 'REG'
    })

    // how do i remove the "budgt" from each remaining totype
  })
})

sample data

var data = [
  {
    org: "org1",
    occs: [
      {
        name: "occ1",
        totypes: [
          {
            id: "REG",
            act: 1,
            auth: 2,
            budgt: 3
          },
          {
            id: "PRV",
            act: 1,
            auth: 2,
            budgt: 3
          }
        ]
      },
      {
        name: "occ2",
        totypes: [
          {
            id: "REG",
            act: 1,
            auth: 2,
            budgt: 3
          },
          {
            id: "PRV",
            act: 1,
            auth: 2,
            budgt: 3
          }
        ]
      },
  },
  {
    org: "org2",
    occs: [
      {
        name: "occ1",
        totypes: [
          {
            id: "REG",
            act: 1,
            auth: 2,
            budgt: 3
          },
          {
            id: "PRV",
            act: 1,
            auth: 2,
            budgt: 3
          }
        ]
      },
      {
        name: "occ2",
        totypes: [
          {
            id: "REG",
            act: 1,
            auth: 2,
            budgt: 3
          },
          {
            id: "PRV",
            act: 1,
            auth: 2,
            budgt: 3
          }
        ]
      }
    ]
  }
]

There is no way to "deep remove" the way you are describing with Lodash. It doesn't provide any querying construct like MongoDB.

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