1

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.

1
  • your arr2 and expected arr are both same, please provide more details Commented Feb 10, 2023 at 8:36

3 Answers 3

2

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)

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

Comments

1

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

Comments

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

})

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.