Home > Blockchain >  Compare two array of objects and modify original array of object
Compare two array of objects and modify original array of object

Time:10-19

Let we have two array of objects as,

let oldArrayOfObject = [
    {
      Item: "ACC",
      Price: "",
      hasPrice: false,
    },
    {
      Item: "BCC",
      Price: "",
      hasPrice: false,
    },
    {
      Item: "CCC",
      Price: "",
      hasPrice: false,
    },
    {
      Item: "DCC",
      Price: "",
      hasPrice: false,
    },
    {
      Item: "ECC",
      Price: "",
      hasPrice: false,
    },
  ];

let newArrayOfObject = [
  {
    Item: "ACC",
    Price: 12,
  },
  {
    Item: "BCC",
    Price: 50,
  },
  {
    Item: "ECC",
    Price: 21,
  }
];

Compare two array of objects and if price exists in newArrayOfObject for particular Item insert price into oldArrayOfObject for that particular item and set hasPrice: true.

Expected O/P:

console.log(oldArrayOfObject)

[
    {
      Item: "ACC",
      Price: 12,
      hasPrice: true,
    },
    {
      Item: "BCC",
      Price: 50,
      hasPrice: true,
    },
    {
      Item: "CCC",
      Price: "",
      hasPrice: false,
    },
    {
      Item: "DCC",
      Price: "",
      hasPrice: false,
    },
    {
      Item: "ECC",
      Price: 21,
      hasPrice: true,
    },
  ];

For this I tried as,

const modifiedArrayOfObject = newArrayOfObject.map((node) => {
  const oldInfo = oldArrayOfObject.find((item) => item.Item === node.Item);
  if (oldInfo) {
    return { ...node, hasPrice: oldInfo.Price !==  node.Price }
  } else {
     return { ...node, hasPrice: true };
  }
});

But I am unable to move forward from here. Please let me know if anyone needs any further explanation or further clearance.

CodePudding user response:

You can try use this function in example:

let oldArrayOfObject = [
    {
      Item: "ACC",
      Price: "",
      hasPrice: false,
    },
    {
      Item: "BCC",
      Price: "",
      hasPrice: false,
    },
    {
      Item: "CCC",
      Price: "",
      hasPrice: false,
    },
    {
      Item: "DCC",
      Price: "",
      hasPrice: false,
    },
    {
      Item: "ECC",
      Price: "",
      hasPrice: false,
    },
  ];

let newArrayOfObject = [
  {
    Item: "ACC",
    Price: 12,
  },
  {
    Item: "BCC",
    Price: 50,
  },
  {
    Item: "ECC",
    Price: 21,
  }
];

function modifiedArrayOfObject(item) {
   newArrayOfObject.map(v => {
    if(v.Item === item){
      const index = oldArrayOfObject.findIndex(obj => obj.Item === v.Item);
      oldArrayOfObject[index] = { Item: v.Item, Price: v.Price, hasPrice: true };
    }
   })
 console.log("oldArrayOfObject:", oldArrayOfObject)
}
modifiedArrayOfObject("ACC")
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

If you plan to modify oldArrayOfObject then I would not suggest using map for this. Instead use a simple for loop -

const oldArrayOfObject = ...

const newArrayOfObject = ...

for (const t of newArrayOfObject) {
  const q = oldArrayOfObject.find(o => o.Item=== t.Item)
  if (q == null) continue
  q.Price = t.Price
  q.hasPrice = true
}

console.log(oldArrayofObject)

CodePudding user response:

I think you confuse newArrayOfObjects and oldArrayOfObjects. This is solution for your way:

const modifiedArrayOfObject = oldArrayOfObject.map((node) => {
    const newInfo = newArrayOfObject.find((item) => item.Item === node.Item);
    if (newInfo) return { ...node, hasPrice: true, Price: newInfo.Price };
    return node;
});

CodePudding user response:

I wouldn't recommend you to change any of the arrays, leave them immutable and create a new array unifying them. You were on the right track, however, as your oldest list has more items, you should go through it instead of the new one, and if that scenario changes, your code would break.

const modifiedArrayOfObject = oldArrayOfObject.map((oldInfo) => {
  const newInfo = newArrayOfObject.find((newInfo) =>  newInfo.Item === oldInfo.Item);
  if (newInfo && newInfo.Price) {
    return { ...oldInfo, Price: newInfo.Price, hasPrice: true };
  }

  return { ...oldInfo, hasPrice: false };
});


console.log("modifiedArrayOfObject:", modifiedArrayOfObject);

Also, don't use map without return.

CodePudding user response:

The issue with your code were

  • You have to find the nodes from oldArrayOfObject in newArrayOfObject. Your oldInfo should be defined as oldInfo = newArrayOfObject.find((item) => item.Item === node.Item);
  • Also if oldInfo is found, you should reurn the Price as Price: oldInfo.Price
  • Incase if the oldInfo is not found, you have to return the current node itself. Dont set hasPrice: true manually in that.

Working Fiddle

const oldArrayOfObject = [{ Item: "ACC", Price: "", hasPrice: false, }, { Item: "BCC", Price: "", hasPrice: false, }, { Item: "CCC", Price: "", hasPrice: false, }, { Item: "DCC", Price: "", hasPrice: false, }, { Item: "ECC", Price: "", hasPrice: false, }];

const newArrayOfObject = [{ Item: "ACC", Price: 12, }, { Item: "BCC", Price: 50, }, { Item: "ECC", Price: 21 }];

const modifiedArrayOfObject = oldArrayOfObject.map((node) => {
    const oldInfo = newArrayOfObject.find((item) => item.Item === node.Item);
    if (oldInfo) {
        return { ...node, Price: oldInfo.Price, hasPrice: oldInfo.Price !== node.Price }
    } else {
        return { ...node };
    }
});
console.log(modifiedArrayOfObject);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Array.map always create a new array from an existing array.

If you want to update the original array itself instead of creating a new array, you could run a loop on oldArrayOfObject and check if each node from oldArrayOfObject is there in newArrayOfObject. Then update hasPrice and Price if the matching node is found.

Working Fiddle

const oldArrayOfObject = [{ Item: "ACC", Price: "", hasPrice: false, }, { Item: "BCC", Price: "", hasPrice: false, }, { Item: "CCC", Price: "", hasPrice: false, }, { Item: "DCC", Price: "", hasPrice: false, }, { Item: "ECC", Price: "", hasPrice: false, }];

const newArrayOfObject = [{ Item: "ACC", Price: 12, }, { Item: "BCC", Price: 50, }, { Item: "ECC", Price: 21 }];

oldArrayOfObject.forEach((node) => {
    const oldInfo = newArrayOfObject.find((item) => item.Item === node.Item);
    if (oldInfo) {
        node.hasPrice = oldInfo.Price !== node.Price;
        node.Price = oldInfo.Price;
    }
});
console.log(oldArrayOfObject);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related