首页 > 解决方案 > 根据嵌套对象数组中存在的 id 在嵌套对象数组中添加对象并忽略重复项

问题描述

我有两个对象数组,第一个对象和父对象是“工厂”。FactoryParent 数组包含对象,在该对象列表内有一个产品列表,第二个是产品数组。

首先,我需要使用字段检测工厂nameparent然后我需要检查我的产品数组并取决于字段family.name并将family.parent该特定产品添加到 FactoryParent 数组上的产品数组中,如果对象存在我需要忽略并继续,直到我遍历所有产品数组 ArrayOfProducts

有一个唯一的 UID,如果它存在于 Factory-->Object--->products[] 忽略并检查另一个,

     FactoryParent = [
        {
            "name": "MT",
            "parent": null,
            "Normes": [
                "NF EN 1111"
            ],
            "version_schema": null
        },
        {
            "name": "MS150",
            "parent": "MR",
            "Normes": [
                "NF EN 7777"
            ],
            "products": [
                {
                    "Id": "78789789784dsz654z",
                    "family": {
                        "name": "MS150",
                        "parent": "MR",
                        "gamme": "RAL X",
                        "serie": "something"
                    }
                }
            ]
    
            },
            {
                "name": "ME150",
                "parent": "MM",
                "products": [
                    {
                        "Id": "45d456d44567a8a798a79a87456",
                        "family": {
                            "name": "ME150",
                            "parent": "MM",
                            "gamme": "Lunatic S",
                            "serie": "something"
                        }
                    },
                    {
                        "Id": "60d4b24fea1adc05a6e53e",
                        "family": {
                            "name": "ME150",
                            "parent": "MM",
                            "gamme": "MOMS S",
                            "serie": "somting"
                        }
                    },
                    {
                        "Id": "46f654df564df5645646z",
                        "family": {
                            "name": "ME150",
                            "parent": "MM",
                            "gamme": "RAL S",
                            "serie": "somting"
                        }
                    }
                ]
            }
        ]
    
    
     ArrayOfProducts = [
            {
                "Id": "60d4b24fea1adc05a6e4974e",
                "family": {
                    "name": "ME150",
                    "parent": "MM",
                    "gamme": "DYNAMIC S",
                    "serie": null
                }
            },
            {
                "Id": "sd798d7s8ds756sd4789",
                "family": {
                    "name": "ME150",
                    "parent": "MM",
                    "gamme": "DYNAMIC S",
                    "serie": null
                }
            },
            {
                "Id": "46f654df564df5645646z",
                "family": {
                    "name": "ME150",
                    "parent": "MM",
                    "gamme": "RAL S",
                    "serie": "somting"
                }
            },
            {
                "Id": "88ds456sd4897sd89d7s",
                "family": {
                    "name": "MS150",
                    "parent": "MR",
                    "gamme": "RAL S",
                    "serie": "somting"
                }
            }
        ]

我的最终数组应如下所示:

finalArray = [
    {
        "name": "MT",
        "parent": null,
        "Normes": [
            "NF EN 1111"
        ],
        "version_schema": null
    },
    {
        "name": "MS150",
        "parent": "MR",
        "Normes": [
            "NF EN 7777"
        ],
        "products": [
            {
                "Id": "78789789784dsz654z",
                "family": {
                    "name": "MS150",
                    "parent": "MR",
                    "gamme": "RAL X",
                    "serie": "something"
                }
            },
            {
                "Id": "88ds456sd4897sd89d7s",
                "family": {
                    "name": "MS150",
                    "parent": "MR",
                    "gamme": "RAL S",
                    "serie": "somting"
                }
            }
        ]

        },
        {
            "name": "ME150",
            "parent": "MM",
            "products": [
                {
                    "Id": "45d456d44567a8a798a79a87456",
                    "family": {
                        "name": "ME150",
                        "parent": "MM",
                        "gamme": "Lunatic S",
                        "serie": "something"
                    }
                },
                {
                    "Id": "60d4b24fea1adc05a6e53e",
                    "family": {
                        "name": "ME150",
                        "parent": "MM",
                        "gamme": "MOMS S",
                        "serie": "somting"
                    }
                },
                {
                    "Id": "46f654df564df5645646z",
                    "family": {
                        "name": "ME150",
                        "parent": "MM",
                        "gamme": "RAL S",
                        "serie": "somting"
                    }
                },
                {
                    "Id": "60d4b24fea1adc05a6e4974e",
                    "family": {
                        "name": "ME150",
                        "parent": "MM",
                        "gamme": "DYNAMIC S",
                        "serie": null
                    }
                },
                {
                    "Id": "sd798d7s8ds756sd4789",
                    "family": {
                        "name": "ME150",
                        "parent": "MM",
                        "gamme": "DYNAMIC S",
                        "serie": null
                    }
                },
            ]
        }
    ]

编辑:我做了什么:

origArr = 将包含所有产品的列表。

updatedArr = 将是工厂产品。

  removeDuplicate(origArr: any, updatingArr: any) {
    let ids = new Set(origArr.map((d: { ID: any; }) => d.ID));
    let merged = [...origArr, ...updatingArr.filter((d: { ID: unknown; }) => !ids.has(d.ID))];
  }

ArrayOfProducts.forEach(function (arrayItem) {
   let objectOfFactory = FactoryParent.find((appli: any) => appli.nom === arrayItem.famille.nom);
    console.log(objectOfFactory); // will return the object of the factory array that have the same parent name/name on the product name.

此时,如果不重复,我需要将我的对象添加到该数组中 });

标签: javascripttypescript

解决方案


检查这是否适合您...!

var FactoryParent = [
        {
            "name": "MT",
            "parent": null,
            "Normes": [
                "NF EN 1111"
            ],
            "version_schema": null
        },
        {
            "name": "MS150",
            "parent": "MR",
            "Normes": [
                "NF EN 7777"
            ],
            "products": [
                {
                    "Id": "78789789784dsz654z",
                    "family": {
                        "name": "MS150",
                        "parent": "MR",
                        "gamme": "RAL X",
                        "serie": "something"
                    }
                }
            ]
    
            },{
                "name": "ME150",
                "parent": "MM",
                "products": [
                    {
                        "Id": "45d456d44567a8a798a79a87456",
                        "family": {
                            "name": "ME150",
                            "parent": "MM",
                            "gamme": "Lunatic S",
                            "serie": "something"
                        }
                    },
                    {
                        "Id": "60d4b24fea1adc05a6e53e",
                        "family": {
                            "name": "ME150",
                            "parent": "MM",
                            "gamme": "MOMS S",
                            "serie": "somting"
                        }
                    },
                    {
                        "Id": "46f654df564df5645646z",
                        "family": {
                            "name": "ME150",
                            "parent": "MM",
                            "gamme": "RAL S",
                            "serie": "somting"
                        }
                    }
                ]
            }
        ]
    
    
    var  ArrayOfProducts = [
            {
                "Id": "60d4b24fea1adc05a6e4974e",
                "family": {
                    "name": "ME150",
                    "parent": "MM",
                    "gamme": "DYNAMIC S",
                    "serie": null
                }
            },
            {
                "Id": "sd798d7s8ds756sd4789",
                "family": {
                    "name": "ME150",
                    "parent": "MM",
                    "gamme": "DYNAMIC S",
                    "serie": null
                }
            },
            {
                "Id": "46f654df564df5645646z",
                "family": {
                    "name": "ME150",
                    "parent": "MM",
                    "gamme": "RAL S",
                    "serie": "somting"
                }
            },
            {
                "Id": "88ds456sd4897sd89d7s",
                "family": {
                    "name": "MS150",
                    "parent": "MR",
                    "gamme": "RAL S",
                    "serie": "somting"
                }
            }
        ]

function formatData(){
    return FactoryParent.map(factory => {
        var products = ArrayOfProducts.filter(product =>  factory.name === product.family.name && factory.parent === product.family.parent)
        if(factory?.products){
        products.forEach(product => {
            var isProductExits = factory.products.some(factoryProduct => factoryProduct.Id === product.Id);
            if(!isProductExits){
                factory.products.push(product);
            }
        })
        }
        return factory
        })

}  

console.log(formatData())      

推荐阅读