3

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.

3 Answers 3

1

This is how i would do it:

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);    

You can even refactor the function a bit to take 2 arrays as arguments.... Like this you keep both arrayX and arrayY intact, if you need it...

Sign up to request clarification or add additional context in comments.

Comments

1

You could take an object for all stores and then map new objects with the store names.

This approach requires only two loops.

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; }

1 Comment

Thanks for the effort mate, but this script is not giving the exact array object what I wanted. checkout the first suggestion.
0

I always write as follows. I think it's readable.

    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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.