1

I have this JSON object containing moisture levels for each timestamp like the following:

{
  "values": {
    "21-Aug-2020 20:28:06:611591": "58.59",
    "21-Aug-2020 20:28:09:615714": "71.42",
    "21-Aug-2020 20:28:12:630856": "95.11",
    "21-Aug-2020 20:28:15:640193": "83.69",
    "22-Aug-2020 14:56:31:099964": "54.46",
    "22-Aug-2020 14:56:34:107806": "50.35",
    "22-Aug-2020 14:56:37:109768": "53.78",
    "22-Aug-2020 14:56:40:110309": "72.62"
  }
}

The thing is now that I want to get the first element. If the key name would be "val1" I could access that element by typing ext.val1

 "val1": "58.59",

so the question is: How can I change each key element of my object (dictionary) in an iterative way so I can access them easily?

This is my goal object modification:

{
  "values": {
    "val1": "58.59",
    "val2": "71.42",
    "val3": "95.11",
    "val4": "83.69",
    "val5": "54.46",
    "val6": "50.35",
    "val7": "53.78",
    "val8": "72.62"
  }
}
klevisx
  • 175
  • 1
  • 2
  • 12

1 Answers1

0

You could get a new object by getting the values.

const data = {
  "values": {
    "21-Aug-2020 20:28:06:611591": "58.59",
    "21-Aug-2020 20:28:09:615714": "71.42",
    "21-Aug-2020 20:28:12:630856": "95.11",
    "21-Aug-2020 20:28:15:640193": "83.69",
    "22-Aug-2020 14:56:31:099964": "54.46",
    "22-Aug-2020 14:56:34:107806": "50.35",
    "22-Aug-2020 14:56:37:109768": "53.78",
    "22-Aug-2020 14:56:40:110309": "72.62"
  }
}

data.values = Object.fromEntries(Object.values(data.values).map((v, i) => ['val' + (i + 1), v]));

console.log(data);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • I think this may work for my case. I am trying sth out. Probably I will modify my object in this way. – klevisx Aug 24 '20 at 11:44