合并两个数组对象:一个具有键值对,另一个仅为数组。

3

我有两个数组对象,一个是ArrayX,另一个是ArrayY。 ArrayX包含user_id和store_ids[],ArrayY包含store_id和store_name。 我想根据ArrayX的store_ids合并这两个数组。

```
//First array
ArrayX = [
  {
    user_id: 'user 4',
    store_ids: [ 'store 2','store 4', 'store 1' ],
  },
  {
    user_id: 'user 6',
    store_ids: [ 'store 1', 'store 2' ],
  }
]


//second array
ArrayY = [
  {
    store_id: 'store 4',
    store_name: 'store D'
  },
  {
    store_id: 'store 2',
    store_name: 'store B'
  },
  {
    store_id: 'store 1',
    store_name: 'store A'
  },
  {
    store_id: 'store 3',
    store_name: 'store C'
  }
] 
```

and what i wanted is given below.

```
ArrayZ = [
  {
    user_id: 'user 4',
    store_ids: [ 'store 2','store 4', 'store 1' ],
    store_info : [
            {
                    store_id: 'store 2',
                    store_name: 'store B'
            },
            {
                    store_id: 'store 4',
                    store_name: 'store D'
            },
            {
                    store_id: 'store 1',
                    store_name: 'store A'
            }       
        ]
  },
  {
    user_id: 'user 6',
    store_ids: [ 'store 1', 'store 2' ],
    store_info: [
            {
                    store_id: 'store 1',
                    store_name: 'store A',
            },
            {
                store_id: 'store 2',
                store_name: 'store B',
            }
        ]
  }
]
```

我尝试使用map函数,但没有得到上述所提到的期望结果。
let ArrayZ = ArrayX.map((item, i) => Object.assign({}, item, ArrayY[i])) 

[
  {
    user_id: 'user 4',
    store_ids: [ 'store 2', 'store 4','store 1' ],
    store_id: 'store 4',
    store_name: 'store D',
  },
  {
    user_id: 'user 6',
    store_ids: [ 'store 1', 'store 2'],
    store_id: 'store 2',
    store_name: 'store B',
  },
Thi is what i am getting.

有人能在这个问题上提供一些建议吗?

3个回答

1
你可以为所有商店选择一个对象,然后使用商店名称映射新对象。
这种方法只需要两个循环。

const
    array1 = [{ user_id: 'user 4', store_ids: ['store 2', 'store 4', 'store 1'] }, { user_id: 'user 6',  store_ids: ['store 1', 'store 2'] }],
    array2 = [{ store_id: 'store 4', store_name: 'store D' }, { store_id: 'store 2', store_name: 'store B' }, { store_id: 'store 1', store_name: 'store A' }, { store_id: 'store 3', store_name: 'store C' }],
    stores = Object.fromEntries(array2.map(o => [o.store_id, o])),
    result = array1.map(o => ({ ...o, store_info: o.store_ids.map(id => stores[id]) }));

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


感谢你的努力,但是这个脚本没有给出我想要的确切数组对象。请查看第一个建议。 - Vandana Maurya

1
这是我会做的方式:
const arrayZ = [];

const sort = () =>{
    
    arrayX.forEach(element => {
        let store_info = [];
        arrayY.forEach(el =>{
            if(element.store_ids.includes(el.store_id)){
                store_info.push(el);
                console.log(store_info)
            }
        })
        element.store_info = store_info;
        arrayZ.push(element);
    })
} 

sort();

console.log(arrayZ);    


你甚至可以稍微改写一下函数,将两个数组作为参数传递……这样做可以保持arrayX和arrayY的完整性,如果需要的话……

0

我总是这样写。 我认为它很易读。

    const ArrayZ = ArrayX.map((x) => {
      const store_infos = [];
      for (const store_id of x.store_ids) {
        const store_info = ArrayY.find((y) => y.store_id === store_id);
        store_infos.push(store_info);
      }

      return {
        user_id: x.user_id,
        store_ids: x.store_ids,
        store_info: store_infos,
      };
    });

    console.log(JSON.stringify(ArrayZ, null, 2));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接