首页 > 解决方案 > 使用lodash在javascript中省略一个或多个值

问题描述

我有一个复杂的结构,我想从这个结构中省略一些属性以获得最终值

let ListofWorlds = {
    listOfCountries: [{
        add: [{
            id: 1,
            updated: {
                areacode: 123,
                city: {
                    city: {'Austrailia'},
                    houses: {1000}
                }
            }
        }], remove: []
    }]
}

我想从这个结构中省略城市属性并需要这个

let ListofWorlds = {
    listOfCountries: [{
        add: [{
            id: 1,
            updated: {
                areacode: 123
            }
        }], remove: []
    }]
}

这是我尝试过的

let newListOfWorls = _.map(ListofWorlds, function (worlds) {
    return _.omit(worlds, ['city']); })

感谢帮助和知识

标签: javascriptlodash

解决方案


这是我尝试过的。

let ListofWorlds = {
listOfCountries: [{
    add: [{
        id: 1,
        updated: {
            areacode: 123,
            city: {
                city: 'Austrailia',
                houses: 1000
            }
        }
    }], remove: []
}]}

const newList = ListofWorlds.listOfCountries.map(arr=>{ 
arr.add.forEach((item,index)=>{
    arr.add[index] = _.omit(item,'updated.city')
  })
  return arr
})

可能不是最好的方法,但是它可以工作,为什么你的代码不能工作,可能你映射了一个对象ListofWorlds,你需要具体说明你想省略哪个字段


推荐阅读