I am not sure how to form this question, but I will do my best. I want to remove an object from an array produits
that is inside the parent object, depending on the families.name
that must equal a string from the initial object that I want to delete ( I already achieved this part).
Right now I'm stuck and don't know how to update the initial Parent
object, I was able to delete the object from the array produits.
The parent Object:
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
}
]
}
The object I want to delete
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"
}
So that my Parent Object will look like this
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
}
]
}
What I have so far done:
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)
Here you go. This will splice the object with matching name
and uid
from Parent
, like you want.
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)
can't understamd your question properly but if you want to manually remove array then use
delete Parent.familles[0].produits[0]
delete keyword to delete and then path of array to delete.
You can search for the index of the particular element/object and then delete it using
delete Parent.familles[0].produits[0]
const fruits = ["Banana", "Orange", "Apple", "Mango"]; console.log(fruits.indexOf("Apple"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.