[英]Delete an object from an array of objects inside an Object
我不确定如何形成这个问题,但我会尽力而为。 我想从数组中删除一个对象produits
即父对象内,这取决于families.name
即必须等于从我想要删除的初始对象的字符串(I已经达到了这个部分)。
现在我卡住了,不知道如何更新初始Parent
对象,我能够从数组产品中删除该对象。
父对象:
let Parent = {
"Id": "60f03c42a512e04c4b0161e7",
"familles": [{
"name": "LOV D+",
"parent": "QB",
"produits": [{
"uid": "ITEM-20210715-20758",
"type": "fabricant",
"parent_uid": null,
"version_application": "1.1.0",
"creation_date": null,
"titu": "REST1",
"ref_cstb": "162367462",
"gamme": "SUPERGRES",
"serie": "PREMIERE",
"description": "300x300 - 20 - Blue",
"usines": null
},
{
"uid": "QB32N-20210720-75584",
"type": "fabricant",
"parent_uid": null,
"version_application": "1.1.0",
"creation_date": "2021-07-20T14:34:04.49Z",
"titu": "REST1",
"ref_cstb": "ref-18-taieb",
"gamme": "SUPERGRES T_20",
"serie": "WIND",
"description": null,
"usines": null
}
],
"version_schema": null
},
{
"name": "LOV F+",
"parent": "QB",
"produits": [{
"uid": "ITEM-20210720-33547",
"type": "fabricant",
"parent_uid": null,
"version_application": "1.1.0",
"creation_date": "2021-07-20T14:46:37.649Z",
"titu": "REST1",
"ref_cstb": "ref19-taieb",
"gamme": "SUPERGRES T_20",
"serie": "STOCKHOLM",
"description": null,
"usines": null
}],
"version_schema": null
}
]
}
我要删除的对象
let obj1 = {
"uid": "ITEM-20210715-20758",
"type": "fabricant",
"parent_uid": null,
"version_application": "1.1.0",
"creation_date": null,
"titu": "REST1",
"ref_cstb": "162367462",
"gamme": "SUPERGRES",
"serie": "PREMIERE",
"description": "300x300 - 20 - Blue",
"usines": null,
"group": "QB - LOV D+",
"parent": "QB"
}
这样我的父对象看起来像这样
let Parent = {
"Id": "60f03c42a512e04c4b0161e7",
"familles": [{
"name": "LOV D+",
"parent": "QB",
"produits": [{
"uid": "ITEM-20210720-75584",
"type": "fabricant",
"parent_uid": null,
"version_application": "1.1.0",
"creation_date": "2021-07-20T14:34:04.49Z",
"titu": "REST1",
"ref_cstb": "ref-18-taieb",
"gamme": "SUPERGRES T_20",
"serie": "WIND",
"description": null,
"usines": null
}],
"version_schema": null
},
{
"name": "LOV F+",
"parent": "QB",
"produits": [{
"uid": "ITEM-20210720-33547",
"type": "fabricant",
"parent_uid": null,
"version_application": "1.1.0",
"creation_date": "2021-07-20T14:46:37.649Z",
"titu": "REST1",
"ref_cstb": "ref19-taieb",
"gamme": "SUPERGRES T_20",
"serie": "STOCKHOLM",
"description": null,
"usines": null
}],
"version_schema": null
}
]
}
到目前为止我所做的:
Remove(obj, parentObj) {
let a = e.group.split("-").pop().slice(1)
console.log(a) // Will Display LOV D+
let newArray = this.certificat.familles.find((x: {
name: any;
}) => x.name === a); // Will display the proper object that have the name = LOV D+
newArray = newArray.produits.filter(({
uid
}) => {
return uid !== e.uid;
}); // This will delete the object from the array, but it will not update the parent object, I'm missing something here
}
Remove(obj1, parent)
干得好。 这将拼接具有匹配name
和来自Parent
uid
的对象,如您所愿。
let Parent = { "Id": "60f03c42a512e04c4b0161e7", "familles": [{ "name": "LOV D+", "parent": "QB", "produits": [{ "uid": "ITEM-20210715-20758", "type": "fabricant", "parent_uid": null, "version_application": "1.1.0", "creation_date": null, "titu": "REST1", "ref_cstb": "162367462", "gamme": "SUPERGRES", "serie": "PREMIERE", "description": "300x300 - 20 - Blue", "usines": null }, { "uid": "QB32N-20210720-75584", "type": "fabricant", "parent_uid": null, "version_application": "1.1.0", "creation_date": "2021-07-20T14:34:04.49Z", "titu": "REST1", "ref_cstb": "ref-18-taieb", "gamme": "SUPERGRES T_20", "serie": "WIND", "description": null, "usines": null } ], "version_schema": null }, { "name": "LOV F+", "parent": "QB", "produits": [{ "uid": "ITEM-20210720-33547", "type": "fabricant", "parent_uid": null, "version_application": "1.1.0", "creation_date": "2021-07-20T14:46:37.649Z", "titu": "REST1", "ref_cstb": "ref19-taieb", "gamme": "SUPERGRES T_20", "serie": "STOCKHOLM", "description": null, "usines": null }], "version_schema": null } ] } let obj1 = { "uid": "ITEM-20210715-20758", "type": "fabricant", "parent_uid": null, "version_application": "1.1.0", "creation_date": null, "titu": "REST1", "ref_cstb": "162367462", "gamme": "SUPERGRES", "serie": "PREMIERE", "description": "300x300 - 20 - Blue", "usines": null, "group": "QB - LOV D+", "parent": "QB" } function Remove(obj, parentObj) { let famille = parentObj.familles.findIndex((x) => x.name === obj.group.split(" - ").pop()); let produit = parentObj.familles[famille].produits.findIndex((x) => x.uid === obj.uid); parentObj.familles[famille].produits.splice(produit, 1); } Remove(obj1, Parent); console.log(Parent);
let Parent = { "Id": "60f03c42a512e04c4b0161e7", "familles": [{ "name": "LOV D+", "parent": "QB", "produits": [{ "uid": "ITEM-20210715-20758", "type": "fabricant", "parent_uid": null, "version_application": "1.1.0", "creation_date": null, "titu": "REST1", "ref_cstb": "162367462", "gamme": "SUPERGRES", "serie": "PREMIERE", "description": "300x300 - 20 - Blue", "usines": null }, { "uid": "QB32N-20210720-75584", "type": "fabricant", "parent_uid": null, "version_application": "1.1.0", "creation_date": "2021-07-20T14:34:04.49Z", "titu": "REST1", "ref_cstb": "ref-18-taieb", "gamme": "SUPERGRES T_20", "serie": "WIND", "description": null, "usines": null } ], "version_schema": null }, { "name": "LOV F+", "parent": "QB", "produits": [{ "uid": "ITEM-20210720-33547", "type": "fabricant", "parent_uid": null, "version_application": "1.1.0", "creation_date": "2021-07-20T14:46:37.649Z", "titu": "REST1", "ref_cstb": "ref19-taieb", "gamme": "SUPERGRES T_20", "serie": "STOCKHOLM", "description": null, "usines": null }], "version_schema": null } ] } let obj1 = { "uid": "ITEM-20210715-20758", "type": "fabricant", "parent_uid": null, "version_application": "1.1.0", "creation_date": null, "titu": "REST1", "ref_cstb": "162367462", "gamme": "SUPERGRES", "serie": "PREMIERE", "description": "300x300 - 20 - Blue", "usines": null, "group": "QB - LOV D+", "parent": "QB" } function filterParent(obj, parentObj) { let a = obj.group.split("-").pop().slice(1) let newArray = parentObj.familles.map((x) => x.name !== a ? x : {...x, produits:x.produits.filter(matchUID)}); function matchUID(produit){ return produit.uid !== obj.uid } return {...parentObj, familles:newArray} } Parent = filterParent(obj1, Parent) console.log(Parent)
无法正确理解您的问题,但如果您想手动删除数组,请使用
删除 Parent.familles[0].produits[0]
delete 关键字要删除,然后是要删除的数组的路径。
您可以搜索特定元素/对象的索引,然后使用
删除 Parent.familles[0].produits[0]
const fruits = ["Banana", "Orange", "Apple", "Mango"]; console.log(fruits.indexOf("Apple"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.