我有两个数组对象,一个是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.
有人能在这个问题上提供一些建议吗?