3

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

2
  • Just making sure I have this right. So your API is pushing out a giant object and not an array with objects? Commented Mar 31, 2021 at 18:21
  • Yes exactly. The top level is an object Commented Mar 31, 2021 at 18:27

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Apologies for the lack of code, i have added it now, and thanks for your advice
Don't worry. I'm seeing that there is people already trying to help you, my next advice will be to solve this on your own. It is one of those things that are simple once you get it right, seeing the other answers you can see what are the other people trying and you could use that for your own solution. In my opinion if someone gives you a working code that you only copy/paste then you will be missing the learning opportunity.
1

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)

2 Comments

This works fantastic and is far the fastest performing answer! Thanks ever so much!!
@AndreaCorrado Happy to help.
1

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

9 Comments

This is great thanks. However i do not get the same output as you, what you have is correct.. This is the output i get: imgur.com/JjLT6Yi
you want Data as an array or an object?
I feel like the id needs to be outside the second map, so that there is only one ID and then data in the next array.
This is how it should look, imagine also there is multiple other device sets as in this example in the picture there is only one. imgur.com/u7Y1Vog
OK Ive updated to what i think you want. It could be cleaner, but there you go...
|
0
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.

1 Comment

Thanks, but i do not really understand the output of this at all? It seems to only output a small array = [1,2]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.