我有两个数组对象,数组 A 有存储组对象,数组 B 有存储对象

I have two array object, array A have store groups object and array B have stores object

第一个数组包含具有唯一性的对象 store_group_id.

let arrayA = [
  {
    store_group_id: 'ID0-y6z-85',
    store_group_name: 'Store Group A',
    store_group_desc: 'This is the desc of store group A'
  },
  {
    store_group_id: 'ID4-y7z-27',
    store_group_name: 'Store Group B',
    store_group_desc: 'This is the desc of store group B'
  }
]

第二个数组包含在 storeGroups.

中具有 store_group_ids 的商店对象
let arrayB = [
  {
    store_id: 'store 1',
    store_name: 'store A',
    description: 'This is the description of store 1',
    org_id: 'org id 1',
    storeGroups: [ 'ID0-y6z-85', 'ID4-y7z-27' ]
  },
  {
    store_id: 'store 2',
    store_name: 'store B',
    description: 'This is the description of store 2',
    org_id: 'org id 1',
    storeGroups: [ 'ID0-y6z-85', 'ID4-y7z-27' ]
  },
  {
    store_id: 'store 5',
    store_name: 'store E',
    description: 'This is the description of store 5',
    org_id: 'org id 1',
    storeGroups: [ 'ID0-y6z-85' ]
  }
] 

我想要一个数组对象映射到两个数组的存储_group_id。 我想要结果数组如下所示。

arrayResult = [
    {
        "store_group_id": "ID0-y6z-85",
        "store_group_name": "Store Group A",
        "store_group_desc": "This is the desc of store group A",
        "AllstoreGroups": [
            {
                "store_id": "store 1",
                "store_name": "store A",
                "description": "This is the description of store 1",
                "org_id": "org id 1",
                "storeGroups": [
                    "ID0-y6z-85",
                    "ID4-y7z-27"
                ]
            },
            {
                "store_id": "store 2",
                "store_name": "store B",
                "description": "This is the description of store 2",
                "org_id": "org id 1",
                "storeGroups": [
                    "ID0-y6z-85",
                    "ID4-y7z-27"
                ]
            },
            {
                "store_id": "store 5",
                "store_name": "store E",
                "description": "This is the description of store 5",
                "org_id": "org id 1",
                "storeGroups": [
                    "ID0-y6z-85"
                ]
            }
        ]
    },
    {
        "store_group_id": "ID4-y7z-27",
        "store_group_name": "Store Group B",
        "store_group_desc": "This is the desc of store group B",
        "AllstoreGroups": [
   {
                "store_id": "store 1",
                "store_name": "store A",
                "description": "This is the description of store 1",
                "org_id": "org id 1",
                "storeGroups": [
                    "ID0-y6z-85",
                    "ID4-y7z-27"
                ]
            },
            {
                "store_id": "store 2",
                "store_name": "store B",
                "description": "This is the description of store 2",
                "org_id": "org id 1",
                "storeGroups": [
                    "ID0-y6z-85",
                    "ID4-y7z-27"
                ]
            }
         ]
    }
]

我尝试了 for 循环和映射,但没有得到所需的数组对象。 谁能帮我解决这个问题?

只需遍历数组 A 和 B,然后检查它们的 store_group_id,如下所示:

const arrayA = [
  {
    store_group_id: "ID0-y6z-85",
    store_group_name: "Store Group A",
    store_group_desc: "This is the desc of store group A",
  },
  {
    store_group_id: "ID4-y7z-27",
    store_group_name: "Store Group B",
    store_group_desc: "This is the desc of store group B",
  },
]

const arrayB = [
  {
    store_id: "store 1",
    store_name: "store A",
    description: "This is the description of store 1",
    org_id: "org id 1",
    storeGroups: ["ID0-y6z-85", "ID4-y7z-27"],
  },
  {
    store_id: "store 2",
    store_name: "store B",
    description: "This is the description of store 2",
    org_id: "org id 1",
    storeGroups: ["ID0-y6z-85", "ID4-y7z-27"],
  },
  {
    store_id: "store 5",
    store_name: "store E",
    description: "This is the description of store 5",
    org_id: "org id 1",
    storeGroups: ["ID0-y6z-85"],
  },
]

for (let i = 0; i < arrayA.length; i++) {
  const currA = arrayA[i]

  for (let j = 0; j < arrayB.length; j++) {
    const currB = arrayB[j]

    if (currB.storeGroups.includes(currA.store_group_id)) {
      if (currA.AllstoreGroups === undefined) {
        currA.AllstoreGroups = []
      }

      currA.AllstoreGroups.push(currB)
    }
  }
}

console.log(arrayA)

可能会映射数组然后过滤数组以达到您预期的结果,这里是一个示例

let arrayA = [
  {
    store_group_id: 'ID0-y6z-85',
    store_group_name: 'Store Group A',
    store_group_desc: 'This is the desc of store group A'
  },
  {
    store_group_id: 'ID4-y7z-27',
    store_group_name: 'Store Group B',
    store_group_desc: 'This is the desc of store group B'
  }
];

let arrayB = [
  {
    store_id: 'store 1',
    store_name: 'store A',
    description: 'This is the description of store 1',
    org_id: 'org id 1',
    storeGroups: [ 'ID0-y6z-85', 'ID4-y7z-27' ]
  },
  {
    store_id: 'store 2',
    store_name: 'store B',
    description: 'This is the description of store 2',
    org_id: 'org id 1',
    storeGroups: [ 'ID0-y6z-85', 'ID4-y7z-27' ]
  },
  {
    store_id: 'store 5',
    store_name: 'store E',
    description: 'This is the description of store 5',
    org_id: 'org id 1',
    storeGroups: [ 'ID0-y6z-85' ]
  }
];

let expectedArray = arrayA.map((item, index) => {
    let allStoreGroups = arrayB.filter((groupItem, groupIndex) => {
        return groupItem.storeGroups.includes(item.store_group_id);
    });
    
    item.AllStoreGroups = allStoreGroups;
    return item;
})

console.log(JSON.stringify(expectedArray));

尝试这样的事情:

let arrayA = [
  {
    store_group_id: "ID0-y6z-85",
    store_group_name: "Store Group A",
    store_group_desc: "This is the desc of store group A"
  },
  {
    store_group_id: "ID4-y7z-27",
    store_group_name: "Store Group B",
    store_group_desc: "This is the desc of store group B"
  }
];

let arrayB = [
  {
    store_id: "store 1",
    store_name: "store A",
    description: "This is the description of store 1",
    org_id: "org id 1",
    storeGroups: ["ID0-y6z-85", "ID4-y7z-27"]
  },
  {
    store_id: "store 2",
    store_name: "store B",
    description: "This is the description of store 2",
    org_id: "org id 1",
    storeGroups: ["ID0-y6z-85", "ID4-y7z-27"]
  },
  {
    store_id: "store 5",
    store_name: "store E",
    description: "This is the description of store 5",
    org_id: "org id 1",
    storeGroups: ["ID0-y6z-85"]
  }
];

const arrC = arrayA.map((item) => {
  return {
    ...item,
    AllstoreGroups: getStoreGroups(item)
  };
});

function getStoreGroups(item) {
  const itemInStore = [];

  arrayB.forEach((element) => {
    if (element.storeGroups.includes(item.store_group_id)) {
      itemInStore.push(element);
    }
  });

  return itemInStore;
}

console.log(arrC)

通过这种方式,您只需将 arrayA 映射为具有您想要的结构,然后使用 getStoreGroups,您可以在 arrayB 内部观察以找到具有当前 arrayAItem store_group_idstoreGroups 数组中。

如果您想清理代码,您还可以在 getStoreGroups 函数中使用 filter 函数而不是 forEach

function getStoreGroups(item) {
  const itemInStore = arrayB.filter((element) =>
    element.storeGroups.includes(item.store_group_id)
  );

  return itemInStore;
}

这个解决方案只需要两个循环(并且不应该为这个工作花费更多)。

  1. 按组收集所有商店。
  2. 映射一个包含组及其关联商店的数组。

const
    arrayA = [{ store_group_id: 'ID0-y6z-85', store_group_name: 'Store Group A', store_group_desc: 'This is the desc of store group A' }, { store_group_id: 'ID4-y7z-27', store_group_name: 'Store Group B', store_group_desc: 'This is the desc of store group B' }],
    arrayB = [{ store_id: 'store 1', store_name: 'store A', description: 'This is the description of store 1', org_id: 'org id 1', storeGroups: ['ID0-y6z-85', 'ID4-y7z-27']}, { store_id: 'store 2', store_name: 'store B', description: 'This is the description of store 2', org_id: 'org id 1', storeGroups: ['ID0-y6z-85', 'ID4-y7z-27'] }, { store_id: 'store 5', store_name: 'store E', description: 'This is the description of store 5', org_id: 'org id 1', storeGroups: ['ID0-y6z-85'] }] ,
    storeGroups = arrayB.reduce((r, o) => o.storeGroups.reduce((q, id) => ((q[id] ??= []).push(o), q), r), {}),
    result = arrayA.map(o => ({ ...o, stores: storeGroups[o.store_group_id] || [] }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }