javascript - 使用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']); })
感谢帮助和知识
解决方案
这是我尝试过的。
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
,你需要具体说明你想省略哪个字段
推荐阅读
- mysql - 如何统计每天的人数并使用 SQL 对他们进行分组?
- python - 将参数传递给 docker-compose 命令
- azure-site-recovery - Site Recovery - Powershell 通过 Azure 自动化“重新保护” - 不工作(Azure 到 Azure)
- javascript - ERESOLVE 无法解析安装 npm 包 @react-native-community/cameraroll 的依赖关系树
- excel - “运行时错误'1004':我们无法粘贴此数据,因为它花费了太长时间。请尝试减小选择的大小。”
- flutter - Flutter 从 JSON 导出 pdf
- html - 关于excel VBA导航网站html的问题
- python - 是否可以在没有参考的情况下加入 2 个 django 模型?
- javascript - 为什么 req.body 返回一个空对象?
- c# - TaskContinuations 与异步 lambda 同步运行