简体   繁体   English

从对象内的对象数组中删除对象

[英]Delete an object from an array of objects inside an Object

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).我想从数组中删除一个对象produits即父对象内,这取决于families.name即必须等于从我想要删除的初始对象的字符串(I已经达到了这个部分)。

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.现在我卡住了,不知道如何更新初始Parent对象,我能够从数组产品中删除该对象。

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.这将拼接具有匹配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)

can't understamd your question properly but if you want to manually remove array then use无法正确理解您的问题,但如果您想手动删除数组,请使用

delete Parent.familles[0].produits[0]删除 Parent.familles[0].produits[0]

delete keyword to delete and then path of array to delete. delete 关键字要删除,然后是要删除的数组的路径。

You can search for the index of the particular element/object and then delete it using您可以搜索特定元素/对象的索引,然后使用

delete Parent.familles[0].produits[0]删除 Parent.familles[0].produits[0]

 const fruits = ["Banana", "Orange", "Apple", "Mango"]; console.log(fruits.indexOf("Apple"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM