I recognize that this has been talked about, but none of the given answers are working. This is an async function.
const activeOrders = await Order.getActiveOrders()
const orderIds = activeOrders.map((order) => { return order.clientId })
const fullClients = await clientController.getClientsByIdArray(orderIds)
for (let j = 0; j < activeOrders.length; ++j) {
for (let i = 0; i < fullClients.length; ++i) {
if (String(activeOrders[j].clientId) === String(fullClients[i]._id)) {
activeOrders[j]['client'] = fullClients[i] // THIS IS NOT WORKING
console.log(activeOrders[j].client) // THIS CORRECTLY DISPLAYS fullClients[i]
console.log(activeOrders[j]) // THIS SHOWS THE OBJECT BUT DOES NOT
// INCLUDE activeOrders[j].client
}
}
}
console.log(activeOrders[0]) // HERE activeOrders[j].client IS MISSING (there is currently 1 item in each array)
const arr1 = [{stuff: "Some stuff", id: 6}]
const arr2 = [{otherIsh: "Sysreq", id: 6}]
for (let i = 0; i < arr1.length; ++i) {
for (let j = 0; j < arr2.length; ++j) {
if (arr1[i].id === arr2[j].id) {
arr1[i].newObj = arr2[j]
break
}
}
}
console.log(arr1[0])
I really don't get how it can be there in the first console.log, but then not exist in the next. In this snippet, I do the same thing and it works just fine.