0

I need to access data in this format to use it with Chart.js:

var dataArrays = {
  id: 1,
  time : [1543855884861,1543855889279],
  parsetTimestamp: ["3 Dec 2018 17:51:24","3 Dec 2018 17:51:29"],
  temperature: [23.4,24.4],
  humidity: [38,40],
  light: [1000,1100],
  soil: [750,800],
  fanIn: [1,0],
  fanOut: [0,1],
  ledIntensity: [1024,1024]
 }

I need to get the data from a different js file, formatted like this:

var data = [{
    name: 'tube',
    time: 1546614557692,
    temperature: 23,
    humidity: 49,
    light: 1718,
    soil: 1024,
    fanIn: 0,
    fanOut: 0,
    ledIntensity: 1024
  },
  {
    name: 'tube',
    time: 1546614557697,
    temperature: 23,
    humidity: 49,
    light: 1721,
    soil: 1024,
    fanIn: 0,
    fanOut: 0,
    ledIntensity: 1024
  }
]

I think I can add data from different variables like this:

var dataArrays.time = [data1.time, data2.time];

but I don't have two variables, I only have one with all the data, and it's located in a different file.

Is there a way to do this?

Ivan
  • 11,733
  • 5
  • 35
  • 63
DaanR
  • 23
  • 8
  • I have noticed there is a difference between 'id' and 'name', I need to use 'name' from the variable data, I'll fix that in my code – DaanR Jan 05 '19 at 11:00
  • So you want the object property values in `dataArrays` to consist of object properties from `data`? – AndrewL64 Jan 05 '19 at 11:00

3 Answers3

1

If you simply want to merge the objects of the array, you can use Array.prototype.reduce to do so.

Just iterate through the keys of the object contained in the array data and merge the duplicate keys and put them in array and assign it to the accumulated merged object.

var data = [{
    name: 'tube',
    time: 1546614557692,
    temperature: 23,
    humidity: 49,
    light: 1718,
    soil: 1024,
    fanIn: 0,
    fanOut: 0,
    ledIntensity: 1024
  },
  {
    name: 'tube',
    time: 1546614557697,
    temperature: 23,
    humidity: 49,
    light: 1721,
    soil: 1024,
    fanIn: 0,
    fanOut: 0,
    ledIntensity: 1024
  }
]

var merged = data.reduce((acc, ele) =>{
for(key in ele){
    acc[key] = acc[key] ? acc[key].concat(ele[key]) : [ele[key]];
}
return acc;
},{});

console.log(merged);
Fullstack Guy
  • 14,425
  • 3
  • 18
  • 35
  • Thank you, this worked. The only problem I have is that the var data is stored in a different file, and because of the amount of data I will receive I don't want to have to write it over, can I reference it in this file in some way? – DaanR Jan 05 '19 at 12:12
  • What I probably should say is that the files are in different folders aswell, one is in `Source code/Frondend` and the other file is in `Source code/Server_Backend` – DaanR Jan 05 '19 at 12:33
  • @DaanR this answer might help you, or you can also serve it over http as a JSON? so that the front end code can parse it :https://stackoverflow.com/questions/23897429/node-import-object-array-from-another-js-file – Fullstack Guy Jan 05 '19 at 12:53
0

Well, you can always create variables according to your needs. Example:

var data1 = data[0];
var data2 = data[1];

And then you can use it as how you've shown in the question.

For accessing different javascript file, if this is in a node runtime, you can use require, if not this might help you

Hope it helps!

Andy Aldo
  • 850
  • 9
  • 24
0

You can use reduce function to combine the data array into one combine object dataArray

var res = {
    id: 1,
    time : [],
    parsetTimestamp: [],
    temperature: [],
    humidity: [],
    light: [],
    soil: [],
    fanIn: [],
    fanOut: [],
    ledIntensity: []
};
dataArray = data.reduce(function(res, cur) {
   res.id = 1,
   res.time.push(cur.time);
   res.temperature.push(cur.temparature);
   res.humidity.push(cur.humidity);
   // etc etc

   return res;  // have to return in reduce
} , res); // initial value
atiqorin
  • 189
  • 1
  • 7