Skip to content
Advertisement

JS ES6/ES10 – conditionally change properties in an Array of Object by another Array of Object

I have a list of orders that I need to modify using another array of objects:

const orders = [
    {
        "shippingCost": 4.23,
        "orderStatus": "PAID",
        "sid": "2AB8FDC19B0E77F738171D8E396BCEE0",
        "creationDate": "2021-09-13T04:17:23",
        "orderId": "12-07303-03670",
        "itemCount": 2,
        "multiOrders": 2,
        "rowNumber": 1,
        "merged": 1
    },
    {
        "shippingCost": 4.29
        "orderStatus": "PAID",
        "sid": "2AB8FDC19B0E77F738171D8E396BCEE0",
        "creationDate": "2021-08-24T16:02:12",
        "orderId": "08-07513-82414",
        "itemCount": 2,
        "multiOrders": 2,
        "rowNumber": 2,
        "merged": 1
    },
    {
        "shippingCost": 6.22,
        "orderStatus": "PAID",
        "sid": "A63E0D42132D3245E8A3B0AAF00B6C3B",
        "creationDate": "2021-07-18T17:50:31",
        "orderId": "16-07301-37416",
        "itemCount": 4,
        "multiOrders": 4,
        "rowNumber": 1,
        "merged": 1
    }
];

const changed=[
    {
        "sid": "D2BD1A6E9DE4C81A3D58138FE35958A0",
        "orderId": "12-07303-03670",
    },
    {
        "sid": "7F9DF7D3D1735BB39CED4866E28FF9F1",
        "orderId": "16-07301-37416",
    }
];

I’m currently using this algorithm to change some values, but I’m (quite) sure there’s a better way to do it: can pls suggest a better or more elegant method?

changed.forEach(i=> {
    let idx=orders.findIndex(o=>o.orderId==i.orderId)
    newData={"sid":i.sid,"rowNumber":1,"itemCount":1,"merged":0}
    Object.assign(this.orders[idx],newData)
    console.log(orders[idx])
})

thanks

Advertisement

Answer

Your code is OK; It does have to look up the orderId every time

If the input array is short or the changes are early in the original array then your code is faster than the example below – if not, not:

const orders = [ { "shippingCost": 4.23, "orderStatus": "PAID", "sid": "2AB8FDC19B0E77F738171D8E396BCEE0", "creationDate": "2021-09-13T04:17:23", "orderId": "12-07303-03670", "itemCount": 2, "multiOrders": 2, "rowNumber": 1, "merged": 1 }, { "shippingCost": 4.29, "orderStatus": "PAID", "sid": "2AB8FDC19B0E77F738171D8E396BCEE0", "creationDate": "2021-08-24T16:02:12", "orderId": "08-07513-82414", "itemCount": 2, "multiOrders": 2, "rowNumber": 2, "merged": 1 }, { "shippingCost": 6.22, "orderStatus": "PAID", "sid": "A63E0D42132D3245E8A3B0AAF00B6C3B", "creationDate": "2021-07-18T17:50:31", "orderId": "16-07301-37416", "itemCount": 4, "multiOrders": 4, "rowNumber": 1, "merged": 1 } ]; const changed=[ { "sid": "D2BD1A6E9DE4C81A3D58138FE35958A0", "orderId": "12-07303-03670", }, { "sid": "7F9DF7D3D1735BB39CED4866E28FF9F1", "orderId": "16-07301-37416", } ];

orders.forEach(item => {
  const change = changed.find(change => item.orderId === change.orderId); 
  if (change)  Object.assign(item, { sid:change.sid, rowNumber : 1, itemCount : 1, merged : 0})
})
  

console.log(orders)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement