4

I have two arrays videos and storeProducts. videos array may have product_id and storeProducts must have product_id. I need merge these based on both produt_id same else only push video items in array.

 let videos = [ { "id": 1, "product_id":"training_video_test_1", "video_title": "Video title 1", "video_short_description": "desc 1" }, { "id": 2, "product_id":"training_video_test_2", "video_title": "Video title 2", "video_short_description": "desc 2" }, { "id": 3, "product_id":"training_video_test_3", "video_title": "Video title 3", "video_short_description": "desc 3" } ]; let storeProducts = [ { "product_id":"training_video_test_1", "prduct_title":"training_video_test_1", "price":100 }, { "product_id":"training_video_test_2", "prduct_title":"training_video_test_2", "price":100 } ]; 

I need to merge these two when storeProducts.product_id === videos.product_id otherswise ignore store products items push only video items.

Example output:

[ { "id": 1, "product_id":"training_video_test_1", "video_title": "Video title 1", "video_short_description": "desc 1", "prduct_title":"training_video_test_1", "price":100 }, { "id": 2, "product_id":"training_video_test_2", "video_title": "Video title 2", "video_short_description": "desc 2", "prduct_title":"training_video_test_2", "price":100 }, { "id": 3, "product_id":"training_video_test_3", "video_title": "Video title 3", "video_short_description": "desc 3" } ] 

I have tried Like this:

let resultArr = []; videos.forEach((v)=>{ storeProducts.forEach((p)=>{ if(p.product_id == v.product_id){ resultArr.push(Object.assign({},v,p)) } }) }) 

But it doesn't work as i expected.Please help me.

2
  • What are you doing when the product_id does not match? Also consider using === for comparison and not == Commented Aug 27, 2020 at 12:56
  • if i do like this: let resultArr = []; videos.forEach((v)=>{ storeProducts.forEach((p)=>{ if(p.product_id === v.product_id){ resultArr.push(Object.assign({},v,p)) }else{ resultArr.push(Object.assign({},p)) } }) }) It duplicates. Commented Aug 27, 2020 at 12:59

3 Answers 3

13

You can use map instead

let videos = [ { id: 1, product_id: "training_video_test_1", video_title: "Video title 1", video_short_description: "desc 1" }, { id: 2, product_id: "training_video_test_2", video_title: "Video title 2", video_short_description: "desc 2" }, { id: 3, product_id: "training_video_test_3", video_title: "Video title 3", video_short_description: "desc 3" }, ]; let storeProducts = [ { product_id: "training_video_test_1", prduct_title: "training_video_test_1", price: 100 }, { product_id: "training_video_test_2", prduct_title: "training_video_test_2", price: 100 }, ]; const result = videos.map(v => ({ ...v, ...storeProducts.find(sp => sp.product_id === v.product_id) })); console.log(result);

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

3 Comments

You are awesome.It works. Can you suggest some advanced array operations tutorials in javascript?. I Mostly use for and forEach.
@Hari Try to familiarize yourself with the methods for working with arrays in ES5
That is a good one. It works perfect for me. Thank you so much.
3

Use spread operator.

let resultArr = []; videos.forEach((v)=>{ storeProducts.forEach((p)=>{ if(p.product_id == v.product_id){ resultArr.push({...v, ...p}) } }) }) 

Comments

3

I recommend you to avoid a find and spread operator for each element within the handler (very poor performance), you can transform the storeProducts array to a key-value object which provides a faster way to access objects.

let videos = [ { "id": 1, "product_id":"training_video_test_1", "video_title": "Video title 1", "video_short_description": "desc 1" }, { "id": 2, "product_id":"training_video_test_2", "video_title": "Video title 2", "video_short_description": "desc 2" }, { "id": 3, "product_id":"training_video_test_3", "video_title": "Video title 3", "video_short_description": "desc 3" }], storeProducts = [ { "product_id":"training_video_test_1", "prduct_title":"training_video_test_1", "price":100 }, { "product_id":"training_video_test_2", "prduct_title":"training_video_test_2", "price":100 }], mapped = storeProducts.reduce((a, c) => (a[c.product_id] = c, a), {}), result = videos.map(o => Object.assign(o, mapped[o.product_id])); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.