从对象内部的对象数组中删除一个对象
2021-07-22
128
我不确定如何提出这个问题,但我会尽力。
我想从父对象内的数组
produits
中删除一个对象,这取决于
families.name
,该对象必须等于我想要删除的初始对象的字符串(我已经实现了这一部分)。
现在我被卡住了,不知道如何更新初始
Parent
对象,我能够从数组 produits 中删除该对象。
父对象:
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)
3个回答
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)
Mohamed Farid
2021-07-22
就这样。这将根据你的需要,从
Parent
中拼接具有匹配
name
和
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);
Timothy Alexis Vass
2021-07-22
您可以搜索特定元素/对象的索引,然后使用
delete Parent.familles[0].produits[0]
const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.indexOf("Apple"))
将其删除
Rajanand
2021-07-22