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

问题描述

一个数组包含具有唯一 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',{
    store_id: 'store 5',store_name: 'store E',description: 'This is the description of store 5',storeGroups: [ 'ID0-y6z-85' ]
  }
] 

我想要一个单一的数组对象与两个数组的 store _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",{
                "store_id": "store 5","store_name": "store E","description": "This is the description of store 5","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","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",{
    store_id: "store 5",store_name: "store E",description: "This is the description of store 5",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)

,

可以先映射arrayA然后过滤arrayB,以达到你预期的结果,这是一个例子

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',{
    store_id: 'store 5',store_name: 'store E',description: 'This is the description of store 5',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_desc: "This is the desc of store group A"
  },store_group_desc: "This is the desc of store group B"
  }
];

let arrayB = [
  {
    store_id: "store 1","ID4-y7z-27"]
  },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 {{1 }} 在 store_group_id 数组中。

如果您想清理代码,您还可以在 storeGroups 函数中使用 filter 函数代替 forEach

getStoreGroups
,

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

  1. 按组收集所有商店。
  2. 将数组映射到组及其关联的商店。

const
    arrayA = [{ store_group_id: 'ID0-y6z-85',store_group_desc: 'This is the desc of store group A' },{ store_group_id: 'ID4-y7z-27',store_group_desc: 'This is the desc of store group B' }],arrayB = [{ store_id: 'store 1',storeGroups: ['ID0-y6z-85','ID4-y7z-27']},{ store_id: 'store 2','ID4-y7z-27'] },{ store_id: 'store 5',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; }