Nick
Nick

Reputation: 771

How to add specific field from different array to every object in curent array

here I have two array of objects which looks like this,

const arr1 =  [
  {
    _id: "63e5cbadd926a20ade863c44",
    productId: "63de474a561e0319a574552b"
  },
  {
    _id: "63e5cbadd926a20ade863c45",
    productId: "63de47c7561e0319a5745531"
  },
  {
    _id: "63e5cbadd926a20ade863c46",
    productId: "63dea93bdf662740f4ba37fe"
  }
]

and other array looks like this,

    const arr2 = [
        {
          _id: "63de474a561e0319a574552b",
          categoryOneId: [Object],
          productPrice: 439.89
        },
        {
          _id: "63de47c7561e0319a5745531",
          categoryOneId: [Object],
          productPrice: 56.9
        },
        {
          _id: "63dea93bdf662740f4ba37fe",
          categoryOneId: [Object],
          productPrice: 56.9
        }
      ]

now I need something like below, where I need all the elements from from array two and need productId field attached to every obj. Please find the expected output below.

[
  {
    _id: "63de474a561e0319a574552b",
    categoryOneId: [Object],
    productPrice: 439.89,
    orderId: "63e5cbadd926a20ade863c44"
  },
  {
    _id: "63de47c7561e0319a5745531",
    categoryOneId: [Object],
    productPrice: 56.9,
    orderId: "63e5cbadd926a20ade863c45"
  },
  {
    _id: "63dea93bdf662740f4ba37fe",
    categoryOneId: [Object],
    productPrice: 56.9,
    orderId: "63e5cbadd926a20ade863c46"
  }
]

I was trying something like this,

for (let i = 0; i < arr2.length; i++) {
      const element = arr2[i];
      if (productIds.find(e => e.productId === element._id )) {
        arr2[i].productId = arr1[i].productId
      }
}

can anyone please help me to fix this.

Thanks.

Upvotes: 1

Views: 80

Answers (3)

GoodMan
GoodMan

Reputation: 611

const arr1 = [{
    _id: "63e5cbadd926a20ade863c44",
    productId: "63de474a561e0319a574552b"
  },
  {
    _id: "63e5cbadd926a20ade863c45",
    productId: "63de47c7561e0319a5745531"
  },
  {
    _id: "63e5cbadd926a20ade863c46",
    productId: "63dea93bdf662740f4ba37fe"
  }
]
const arr2 = [{
    _id: "63de474a561e0319a574552b",
    categoryOneId: "[Object]",
    productPrice: 439.89
  },
  {
    _id: "63de47c7561e0319a5745531",
    categoryOneId: "[Object]",
    productPrice: 56.9
  },
  {
    _id: "63dea93bdf662740f4ba37fe",
    categoryOneId: "[Object]",
    productPrice: 56.9
  }
]

// This attempts to correct your approach
// You did not update the element correctly.
// Iterative Approach
const output = [];
for (let i = 0; i < arr2.length; i++) {
      const element = arr2[i];
      if (item = arr1.find(e => e.productId === element._id )) {
        output.push({ ...element, orderId: item._id})
      }
}

// Idiomatic Javascript  
const output1 = arr2.map(element => {
  if(item = arr1.find(({productId}) => element._id === productId)) {
    return {...element, orderId: item._id}
  }
})

// Functional Style.. eh for learning purposes only. it's an overkill here
const output2 = arr2.reduce((acc, element) => {
  if(item = arr1.find(e => e.productId === element._id )) {
    return acc.concat({...element, orderId: item._id})
  }
}, [])

console.log("Iterative Approach", output)
console.log("Idiomatic Approach", output1)
console.log("Functional Approach", output2)

Upvotes: 2

Jay
Jay

Reputation: 3107

Use map and find

const arr1 = [{
    _id: "63e5cbadd926a20ade863c44",
    productId: "63de474a561e0319a574552b"
  },
  {
    _id: "63e5cbadd926a20ade863c45",
    productId: "63de47c7561e0319a5745531"
  },
  {
    _id: "63e5cbadd926a20ade863c46",
    productId: "63dea93bdf662740f4ba37fe"
  }
]
const arr2 = [{
    _id: "63de474a561e0319a574552b",
    categoryOneId: "[Object]",
    productPrice: 439.89
  },
  {
    _id: "63de47c7561e0319a5745531",
    categoryOneId: "[Object]",
    productPrice: 56.9
  },
  {
    _id: "63dea93bdf662740f4ba37fe",
    categoryOneId: "[Object]",
    productPrice: 56.9
  }
]

const arr = arr2.map(item => ({
  ...item,
  orderId: arr1.find(order => order.productId === item._id)._id,
}))
console.log(arr);

Upvotes: 1

Michael Mishin
Michael Mishin

Reputation: 571

const arr3 = arr2.map((item, key) => ({...item, orderId: arr1[key]._id}))

if arrays are shuffled:

const arr3 = arr2.map((item, key) => {
    const foundItem = arr1.find(e => e.productId === item._id);

    return ({
        ...item, 
        orderId: foundItem ? foundItem._id: null
    });

})

Upvotes: 1

Related Questions