javascript - 根据嵌套对象数组中存在的 id 在嵌套对象数组中添加对象并忽略重复项
问题描述
我有两个对象数组,第一个对象和父对象是“工厂”。FactoryParent 数组包含对象,在该对象列表内有一个产品列表,第二个是产品数组。
首先,我需要使用字段检测工厂name
,parent
然后我需要检查我的产品数组并取决于字段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.
此时,如果不重复,我需要将我的对象添加到该数组中 });
解决方案
检查这是否适合您...!
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())
推荐阅读
- mysql - 在 json 列中使用双引号和反斜杠按键提取值
- python - 尝试使用 SQLAlchemy 关系
- perl - 如何使用 Perl Digest::xxHash 计算文件的哈希值
- python - 如何使用分支运算符在 Airflow DAG 中分支多个路径?
- go - 多端口连接的 UDP 服务器问题
- java - 是否可以动态构造 jOOQ VALUES() 表?
- android-studio - 使用 Kotlin 在 Android Studio 上使用 Spinner 更改活动
- html - Jumbotron 固定图像跳转
- javascript - 未填写数组的特定起始值
- google-apps-script - 通过脚本启用复选框时如何用值替换公式。(谷歌表格)