-4

I read a .json file, parsed it to an js-Object, and set it to an object. But now I want to have two objects. One object remains as it is, and another object can be edited. After editing, I compare these two objects, and in this way, I get all the user changes. OK, now my code:

function Initialization(sPath) {
  _LoadFromFile(sPath).then((oJsObject) => {
     oObject, oObjectEdit = oJsObject;
  }).catch((err) => {
    switch(err) {
       case 'Cannot open file':
          _LoadToFile(sPath).then(() => {
          Initialization(...arguments);
          }).catch((err) => {
        console.log('Err');
          });
        break;
       default:
    console.log('Err');
        }
  });

 function _LoadFromFile(sPath) {
  return new Promise((resolve, reject) => {
    const fileName = sPath.replace(/\n|\r/g, "");
    FileSystem.ReadFile(fileName, 'utf8').then(
      function(ReadFileResolve) {
        resolve(JSON.parse(ReadFileResolve));
        }
      ).catch(function(err) {
    console.log('Err');
        reject(err);
      });
  });

So in witch way I have to set oJsObject to oObject & oObjectEdit? Do I really have to do a copy of oObject ???

oObject = oJsObject;
oObjectEdit = JSON.parse(JSON.stringify(oObject))

Thanks for your support.

JSON Derulo
  • 9,780
  • 7
  • 39
  • 56
  • 1
    what the heck is `.then((oJsObject) => { oObject, oObjectEdit = oJsObject; })` supposed to be doing? by the way, not a `Promise` issue, you want to know how to copy an object, it seems – Jaromanda X Aug 25 '23 at 10:09
  • _"Do i really have to do a copy of oObject ???"_ Yes, you have to create a copy, but you should use the term _"clone"_. – jabaa Aug 25 '23 at 10:10
  • I'm not very familiar with java script syntax. I want to assign the same object to both objects. – noname028743 Aug 25 '23 at 10:10
  • Don't try to guess syntax. Learn the basics and use the syntax you've learned. Guessing won't work. If you need help with cloning: [How do I correctly clone a JavaScript object?](https://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object) – jabaa Aug 25 '23 at 10:11

0 Answers0