gpt4 book ai didi

javascript - 下划线 : How do i delete a nested object by property?

转载 作者:行者123 更新时间:2023-11-30 17:32:11 25 4
gpt4 key购买 nike

我有一个嵌套菜单示例。我想删除具有所选属性的对象。我如何使用下划线或 lodash 来做到这一点?谢谢

[
{
"label": "My Documents",
"id": "mydocs",
"children": [
{
"children": [
{
"children": [

],
"label": "sub folder 1.1",
"id": "58d32eec-75d3-45ab-b896-73bdb12dcacd",
"selected": "selected"
},
{
"children": [
{
"children": [

],
"label": "sub sub folder 1.2.1",
"id": "0c0c9705-7012-4e11-a540-526babdd816f"
}
],
"label": "sub folder 1.2",
"id": "bdfa6eb9-9527-490a-be5d-b2158df98982"
}
],
"label": "folder 1",
"id": "e53455ef-4e0c-4d2c-8148-7e3152fff0ae"
},
{
"children": [

],
"label": "folder 2",
"id": "b6fa392b-89ed-441f-9c4b-2a44c48829f6"
},
{
"children": [

],
"label": "My Docs 2",
"id": "db92b3e6-80f6-4344-bba1-252b195c17a0"
}
]
}
]

最佳答案

你可以尝试这样的事情。

arr = _.without(arr, _.findWhere(arr.children, {selected: 'selected'}));

http://underscorejs.org/#without

关于javascript - 下划线 : How do i delete a nested object by property?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22788020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com