Map nested object to array

Viewed 325

I have been hitting my head against a wall all day trying to solve this problem and also having searched on here i have found nothing as of yet.

My current object that is returned from an API is structured as so, i have over 2000 data points for each device:

{ "DeviceA": [

     {  "device_id": "DeviceA",
        "time": "2021-01-23T18:43:14Z",
        "temperature": 1.875,
        "strength": -14.827574,
        "maturity": 105.904513
     },
     {  "device_id": "ExampleB",
        "time": "2021-01-23T18:53:14Z",
        "temperature": 1.35,
        "strength": -145.64274540827574,
        "maturity": 10.904513
     },
             ]

 "DeviceB": [
     {  "device_id": "DeviceB",
        "time": "2021-01-23T18:43:14Z",
        "temperature": 1.5,
        "strength": -120.64274540827574,
        "maturity": 3233.04513
     },
     {  "device_id": "ExampleB",
        "time": "2021-01-23T18:53:14Z",
        "temperature": 1.875,
        "strength": -17.4274540827574,
        "maturity": 10554.268851904513
     },
}

And how i want it

[
  {
    "id": "DeviceA"
    "Data": [
      {
         "x": "time"
         "y": "temperature"
      }
     ]
  }
  {
    "id": "DeviceB"
    "Data": [
      {
         "x": "time"
         "y": "temperature"
      }
     ]
  }
]

I have tried nesting mapped arrays, millions of things but i can never get the right output. Any help would be massively appreciated!

Edit:

Apologies i had not included some code for what i was working on

  const graphAllTempVTime = () => {
const sensorTempVTimeMapped = Object.entries(sensorDatasFromCast).map(
  (items, index) => ({
    id: items[0],
    data: [
      {
        x: items[1].time,
        y: items[1].temperature,
      },
    ],
  })
)

return sensorTempVTimeMapped
}

However this produces the output of:

enter image description here

4 Answers

You could use: Object.keys() - check the docs It will take an object (like your first example) and returns the keys as an array, from there you could traverse each key and transform each value. You could also check Object.entries() it is newer so be aware of the platform support.

It could be helpful if you post some of the code that you are trying, and maybe the community could spot a fix

Some very simple ways to get started have been mentioned above, but this is the classic functional programming problem in JS. Problems like this are what make my day interesting.

Object.keys(THE_OBEJCT)
  .map(name =>
    THE_OBEJCT[name].map(device => ({
      id: name,
      Data: THE_OBEJCT[name].map(device => ({
        time: device.time,
        temperature: device.temperature
      }))
    }))
  )
  .map(d =>
    d.reduce((memo, d) => ({ id: d.id, data: memo.data.concat(d.Data) }), {
      data: []
    })
  )

Is very basic, but will create an array of objects that look like this...

output

You need to map over the value for each key.

let o = { "DeviceA": [ { "device_id": "DeviceA", "time": "2021-01-23T18:43:14Z", "temperature": 1.875, "strength": -14.827574, "maturity": 105.904513 }, { "device_id": "ExampleB", "time": "2021-01-23T18:53:14Z", "temperature": 1.35, "strength": -145.64274540827574, "maturity": 10.904513 }, ], "DeviceB": [ { "device_id": "DeviceB", "time": "2021-01-23T18:43:14Z", "temperature": 1.5, "strength": -120.64274540827574, "maturity": 3233.04513 }, { "device_id": "ExampleB", "time": "2021-01-23T18:53:14Z", "temperature": 1.875, "strength": -17.4274540827574, "maturity": 10554.268851904513 } ] };
const res = Object.entries(o).map(
  ([key,val], index) => ({
    id: key,
    data: val.map(x=>({x:x.time, y: x.temperature}))
  })
)
console.log(res)

const newArr = []

Object.keys(myObject).map((key, index) => {
  newArr.push(
  {
    id: key,
    Data: [
      {
        x: myObject[key][0].time,
        y: myObject[key][0].temperature,
      }
    ],
  }
  )
});

Something like this perhaps.

Related