Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142646

How to modify the value of an object when it is cloned?

$
0
0

Given the following object

const data = {
  "123-456": {
    "03": 2,
    "04": 0,
    "05": 0,
    "06": 0,
    "07": 0,
    "08": 1,
    "09": 1
  },
  "123-789": {
    "03": 2,
    "04": 0,
    "05": 0,
    "06": 0,
    "07": 0,
    "08": 1,
    "09": 1
  }
};

And the following variables

const value = 10;
const weekday = "03";
const id = "123-456";

I need to create a copy of the data object where the value of the id and day of the given week changes. So given the previous parameters the new object would be the following.

{
  "123-456": {
    "03": 10, // <- here the new value
    "04": 0,
    "05": 0,
    "06": 0,
    "07": 0,
    "08": 1,
    "09": 1
  },
  "123-789": {
    "03": 2,
    "04": 0,
    "05": 0,
    "06": 0,
    "07": 0,
    "08": 1,
    "09": 1
  }
}

And tried to make a copy but without success

This is some of my tries

const newData = { ...data, [id]: { ...data[id], data[id][weekday]: value } };

Thanks for the help


Viewing all articles
Browse latest Browse all 142646

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>