javascript - 从过滤对象数组中删除具有特定键值的对象
问题描述
我有一个对象数组,我过滤它:
const test = [
{
"id":2,
"category_id":1,
"features":["blah","blah","blah","blah"],
"options":
{
"option1":
{
"b":22,"c":105,"d":70,"e":345
},
"option2":
{
"c":25,"c":41,"d":70,"e":345
}
}
},
{
"id":4,
"category_id":2,
"features":["blah","blah","blah","blah"],
"options":
{
"option1":
{
"b":22,"c":105,"d":70,"e":345
},
"option2":
{
"c":25,"c":41,"d":70,"e":345
}
}
},
{
"id":7,
"category_id":4,
"features":null,
"options":null
},
{
"id":11,
"category_id":6,
"features":["blah","blah","blah","blah"],
"options":
{
"option1":
{
"b":22,"c":105,"d":70,"e":345
},
"option2":
{
"c":25,"c":41,"d":70,"e":345
}
}
}
]
const filtered = test.filter((t) => {
})
具有“category_id:4”的对象没有“选项”,但在filter
我需要所有存在的选项值:
const filtered = test.filter((t) => {
const tOptions = Object.values(t.options)
})
它给了我一个错误,因为某些选项不存在。我知道如何修改数组并删除没有选项的对象(使用 Lodash)
_.reject(test, function(el) { return el.category_id === 4 })
但由于后面的逻辑,我无法修改“测试”数组。不知何故,我需要这个结果而不接触数组本身:
[
Object
{
b:22,
c:105,
d:70,
e:345
},
Object
{
c:41,
d: 70,
e: 345
}
]
我真的放弃了。请帮忙。
解决方案
也许你可以尝试复制对象测试使用(..你的对象)或 array.splice(index, number of elements, element, element) 或 slice() 祝你好运
推荐阅读
- computational-geometry - 如何使用镶嵌数据计算点到三角形的最短距离
- javascript - 在 Observable 笔记本中,无法使用 npm 库中的函数,可能是因为无法从库的依赖项中导入函数
- javascript - 如何使用javascript追加/添加元素onclick?
- javascript - 将对象映射到另一个对象 javascript
- html - 为什么我的页脚没有停留在页面底部?
- java - spring-webflux 应用启动失败
- python-3.x - 在python中使用类方法的程序
- android - 剪贴板活动的广播接收器
- flutter - 为什么大部分flutter web包都是用js包开发的?
- scala - Kafka + Spark 错误 MicroBatchExecution:查询