Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parent object changes even if the copy of it is created while splice

I have a parent object categories, and a function that flattens and removes duplicate numbers inside the array.
But even though I make a copy the categories object (let newObj = {...categories}) and perform the above flatten function, it affects the parent categories object.

Code:

let categories = {
  Red: {
    Name: "Fire Flame",
    List: [
      ["1","5"],
      ["10","15"]
    ]
  },
  Blue: {
    Name: "Super Light",
    List: [
      ["1","5"],
      ["9","12"]
    ]
  },
  Custom: {
    List: [
      "Star Flash"
    ]
  }
}



  const submitData = () => {
    let obj = { ...categories };
    let types = Object.keys(obj);
    types.map((key) => {
      flattenAndUnique(key);
    });
    function flattenAndUnique(key) {
      let myList = obj[key].List;
      let resList = [];
      if (key !== "Custom") {
        myList.map(async (data, index) => {
          let min = Math.min(...data);
          let max = Math.max(...data);
          for (let i = min; i <= max; i++) {
            resList.push(String(i));
          }
          const spliced = [...myList.splice(index, 1, resList)];
          let flatLevelArray = [].concat.apply([], myList);
          let uniqueLevelArray = flatLevelArray.filter(
            (v, i, a) => a.indexOf(v) === i
          );
          obj = {
            ...obj,
            [key]: {
              ...obj[key],
              List: uniqueLevelArray,
            },
          };
          resList = [];
        });
      } else {
        let flatLevelArray = [].concat.apply([], myList);
        obj = {
          ...obj,
          Custom: {
            ...obj.Custom,
            List: flatLevelArray,
          },
        };
      }
    }
    console.log("final obj %% ===>", obj);
    console.log("Floors final &&&&", categories);
  };
  
  submitData()

sumbitData function actually flattend the array and removes duplicates. For eg,
If i have two array – [["1","4"] , ["11","13"]], the function transforms it into – ["1","2","3","4","11","12","13"]
which works fine,
My only issue is that my parent categories also changes.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First of all, you get the reference of the List property.

let myList = obj[key].List;

And then in the bottom, you splice it. ( splice will effect the origin array as it has the same referance )

const spliced = [...myList.splice(index, 1, resList)];

It should be

let myList = [...obj[key].List];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading