I have two array objects one is ArrayX and other is ArrayY. ArrayX has user_id and store_ids[] and ArrayY has store_id and store_name I want to merge both the arrays according to the ArrayX's 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',
}
]
}
]
```
I tried the map function but not getting the desired result that is mentioned above.
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.
can anyone suggest something on this.