gpt4 book ai didi

javascript - JS : Remove object from nested array and return parent array

转载 作者:行者123 更新时间:2023-12-01 09:16:45 24 4
gpt4 key购买 nike

如何删除嵌套在另一个数组中的数组 subBrands 中的对象,其中对象的 id 属性为 = 31。我试图在不删除该子品牌的情况下取回整个父数组。

数组是:

[
{
"id": 10,
"name": "Parent Brand 1",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 09:55:51",
"updated_at": "2017-02-02 09:55:51",
"subBrands": [
{
"id": 31,
"name": "Sub Brand 6",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:24:49",
"updated_at": "2017-02-02 11:42:02"
},
{
"id": 32,
"name": "Sub Brand 7",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:24:57",
"updated_at": "2017-02-02 11:42:18"
},
{
"id": 33,
"name": "Sub Brand 8",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:04",
"updated_at": "2017-02-02 11:42:34"
},
{
"id": 34,
"name": "Sub Brand 9",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:39",
"updated_at": "2017-02-02 11:42:43"
},
{
"id": 35,
"name": "Sub Brand 10",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:46",
"updated_at": "2017-02-02 11:42:52"
},
{
"id": 36,
"name": "Sub Brand 4",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:43:53",
"updated_at": "2017-02-02 11:43:53"
}
]
},
{
"id": 12,
"name": "Parent Brand 2",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 09:56:16",
"updated_at": "2017-02-02 09:56:16",
"subBrands": []
},
{
"id": 16,
"name": "Brand no children",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 10:37:40",
"updated_at": "2017-02-02 10:37:40",
"subBrands": []
},
{
"id": 37,
"name": "Whoops brand",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:44:10",
"updated_at": "2017-02-02 11:44:10",
"subBrands": []
}
]

我想要得到的是:
[
{
"id": 10,
"name": "Parent Brand 1",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 09:55:51",
"updated_at": "2017-02-02 09:55:51",
"subBrands": [
{
"id": 32,
"name": "Sub Brand 7",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:24:57",
"updated_at": "2017-02-02 11:42:18"
},
{
"id": 33,
"name": "Sub Brand 8",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:04",
"updated_at": "2017-02-02 11:42:34"
},
{
"id": 34,
"name": "Sub Brand 9",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:39",
"updated_at": "2017-02-02 11:42:43"
},
{
"id": 35,
"name": "Sub Brand 10",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:46",
"updated_at": "2017-02-02 11:42:52"
},
{
"id": 36,
"name": "Sub Brand 4",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:43:53",
"updated_at": "2017-02-02 11:43:53"
}
]
},
{
"id": 12,
"name": "Parent Brand 2",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 09:56:16",
"updated_at": "2017-02-02 09:56:16",
"subBrands": []
},
{
"id": 16,
"name": "Brand no children",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 10:37:40",
"updated_at": "2017-02-02 10:37:40",
"subBrands": []
},
{
"id": 37,
"name": "Whoops brand",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:44:10",
"updated_at": "2017-02-02 11:44:10",
"subBrands": []
}
]

我愿意使用下划线。我最接近的是:
    var brands = _.filter(brands, function(n) { 
return _.some(n.subBrands, function(subBrand){
return subBrand.id != brand.id;
});
});

但这会删除不包含 id 为 31 的子品牌的数组。所以它不是很接近我需要的。

干杯!

最佳答案

var arr = [{"id":10,"name":"Parent Brand 1","parent":null,"author":1,"deleted_at":null,"created_at":"2017-02-02 09:55:51","updated_at":"2017-02-02 09:55:51","subBrands":[{"id":31,"name":"Sub Brand 6","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:24:49","updated_at":"2017-02-02 11:42:02"},{"id":32,"name":"Sub Brand 7","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:24:57","updated_at":"2017-02-02 11:42:18"},{"id":33,"name":"Sub Brand 8","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:25:04","updated_at":"2017-02-02 11:42:34"},{"id":34,"name":"Sub Brand 9","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:25:39","updated_at":"2017-02-02 11:42:43"},{"id":35,"name":"Sub Brand 10","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:25:46","updated_at":"2017-02-02 11:42:52"},{"id":36,"name":"Sub Brand 4","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:43:53","updated_at":"2017-02-02 11:43:53"}]},{"id":12,"name":"Parent Brand 2","parent":null,"author":1,"deleted_at":null,"created_at":"2017-02-02 09:56:16","updated_at":"2017-02-02 09:56:16","subBrands":[]},{"id":16,"name":"Brand no children","parent":null,"author":1,"deleted_at":null,"created_at":"2017-02-02 10:37:40","updated_at":"2017-02-02 10:37:40","subBrands":[]},{"id":37,"name":"Whoops brand","parent":null,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:44:10","updated_at":"2017-02-02 11:44:10","subBrands":[]}];


var id = prompt("Id of subbrands to remove: ");

arr.forEach(function(o) {
o.subBrands = o.subBrands.filter(s => s.id != id);
});

console.log(arr);

关于javascript - JS : Remove object from nested array and return parent array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42005096/

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