-4

so basically I want to compare the object of array with another object and change its value. The data is fully dynamic so I can't use static keys. here is my data

enter image description here

this new DATA is the one I want to add in the invoiceDATA

enter image description here

but the problem is that DOCDT is not static it can be dynamic so I cannot use static key name.

Thank you!!

Owais Ahmed Khan
  • 218
  • 1
  • 4
  • 12
  • With [Object.keys](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) you can get the keys of an object – Reyno Jun 23 '20 at 07:50
  • can you give me code example ? – Owais Ahmed Khan Jun 23 '20 at 07:52
  • 1
    Please post actual code. Screenshots alone do not suffice. – Yoshi Jun 23 '20 at 07:54
  • Does this answer your question? [Object comparison in JavaScript](https://stackoverflow.com/questions/1068834/object-comparison-in-javascript) – Argee Jun 23 '20 at 07:54
  • no @Argee it does not, as i already told my keys and values are dynamic I cannot hardcode it – Owais Ahmed Khan Jun 23 '20 at 07:57
  • 1
    Please don't upload [images of code](https://meta.stackoverflow.com/a/285557/3082296). They can't be copied to reproduce the issue, they aren't searchable for future readers and they are harder to read than text. Please post the actual code **as text** to create a [mcve]. – adiga Jun 23 '20 at 07:58

1 Answers1

1

You can use Object.assign(oldObj, newObj); and it will assign all value from new object to old object. If property is not there then it will also create new properties.

let newObj = { DOCDT: '6/21/20' };
let oldObj = { DOCDT: '1/10/20', DOCNO: "", IREF1: "50" };

Object.assign(oldObj, newObj);
console.log(oldObj);

If oldObj is array of objects then use it like oldObj.forEach(x => Object.assign(x, newObj));.

let newObj = { DOCDT: '6/21/20' };
let oldObj = [{ DOCDT: '1/10/20', DOCNO: "", IREF1: "50" }];

oldObj.forEach(x => Object.assign(x, newObj));
console.log(oldObj);
Karan
  • 12,059
  • 3
  • 24
  • 40