-1

I need to transform data from server format to format for my UI library. But I can't completely understand how I should do this.

Name in new object is a field in object from server except "time" And add "time" value for each fields Need to transform this:

[
 {
 battery_charge: 0

 battery_discharge: 0

 consumption: 404
 
 grid_backflow: 0
 
 grid_consumption: 3
 
 pv_generation: -7
 
 time: "2019年02月25日T00:00:00+00:00"

 }
 {
 battery_charge: 0
 
 battery_discharge: 0
 
 consumption: 404
 
 grid_backflow: 0
 
 grid_consumption: 3
 
 pv_generation: -7
 
 time: "2019年02月25日T10:00:00+00:00"

 }
]

to this:

[
 {
 name: ‘Battery Charge’
 data: [
 { time: '2019-02-25T10:00:00+00:00', value: 3 }
 { time: '2019-02-25T10:00:00+00:00', value: 3 }
 ]
 }
 {
 name: ‘Battery discharge’
 data: [
 { time: '2019-02-25T10:00:00+00:00', value: 3 }
 { time: '2019-02-25T10:00:00+00:00', value: 3 }
 ]
 }
 {
 name: ‘Consumption’
 data: [
 { time: '2019-02-25T10:00:00+00:00', value: 3 }
 { time: '2019-02-25T10:00:00+00:00', value: 3 }
 ]
 }
 {
 name: ‘Grid backflow’
 data: [
 { time: '2019-02-25T10:00:00+00:00', value: 3 }
 { time: '2019-02-25T10:00:00+00:00', value: 3 }
 ]
 }
 {
 name: ‘Grid consumption’
 data: [
 { time: '2019-02-25T10:00:00+00:00', value: 3 }
 { time: '2019-02-25T10:00:00+00:00', value: 3 }
 ]
 }
 {
 name: ‘Pv generation’
 data: [
 { time: '2019-02-25T10:00:00+00:00', value: 3 }
 { time: '2019-02-25T10:00:00+00:00', value: 3 }
 ]
 }
]
asked Nov 17, 2021 at 18:09
5
  • please add your code. what goes wrong? Commented Nov 17, 2021 at 18:19
  • I should mention that I didn't write the code because I don't understand how to do it Commented Nov 17, 2021 at 18:24
  • @OlehStrokan check my answer Commented Nov 17, 2021 at 18:39
  • @programoholic thank you! Commented Nov 17, 2021 at 20:24
  • @OlehStrokan accept if it worked :) Commented Nov 17, 2021 at 20:25

4 Answers 4

1

Here is the simple way to achieve it :

const data = [
 {
 battery_charge: 0,
 battery_discharge: 0,
 consumption: 404,
 grid_backflow: 0,
 grid_consumption: 3,
 pv_generation: -7,
 time: '2019-02-25T00:00:00+00:00',
 },
 {
 battery_charge: 0,
 battery_discharge: 0,
 consumption: 404,
 grid_backflow: 0,
 grid_consumption: 3,
 pv_generation: -7,
 time: '2019-02-25T10:00:00+00:00',
 },
];
const IGNORE_KEY_MAP = {
 time: 1,
};
function transformData(arr) {
 if (arr.length == 0) return [];
 let dataMap = new Map();
 const keys = Object.keys(arr[0]);
 arr.forEach((item) => {
 for (let key of keys) {
 if (!IGNORE_KEY_MAP[key]) {
 let mapValue = dataMap.get(key);
 if (mapValue) {
 let newData = [...mapValue, getInsertableData(item, key)];
 dataMap.set(key, newData);
 } else {
 dataMap.set(key, [getInsertableData(item, key)]);
 }
 }
 }
 });
 let result = [];
 dataMap.forEach((value, key) => {
 const obj = {
 name: key,
 data: value,
 };
 result.push(obj);
 });
 return result;
}
function getInsertableData(data, key) {
 return {
 time: data.time,
 value: data[key],
 };
}
console.log(transformData(data))

Interactive link : Link

answered Nov 17, 2021 at 18:35
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your short code example!
0

I would do this by first transforming into this format:

{
 battery_charge: [
 {time: "2019年02月25日T00:00:00+00:00", value: 0},
 {time: "2019年02月25日T10:00:00+00:00", value: 0}
 ],
 battery_discharge: [ /*...*/ ],
 /* ... */
}

, which is a very common pattern and well-established. Then we can map the entries of this into your name / data format.

We do the first conversion with a double-reduce, and the whole thing might look like this:

const reformat = (data) =>
 Object .entries (data .reduce (
 (a, {time, ...rest}) => Object .entries (rest) .reduce (
 (a, [key, value]) => ((a [key] = [... (a [key] || []), {time, value}]), a)
 , a
 ), {}
 )) .map (([name, data]) => ({name, data}))
const data = [{battery_charge: 0, battery_discharge: 0, consumption: 404, grid_backflow: 0, grid_consumption: 3, pv_generation: -7, time: "2019-02-25T00:00:00+00:00"}, {battery_charge: 0, battery_discharge: 0, consumption: 404, grid_backflow: 0, grid_consumption: 3, pv_generation: -7, time: "2019-02-25T10:00:00+00:00"}]
console .log (reformat (data))
.as-console-wrapper {max-height: 100% !important; top: 0}

answered Nov 18, 2021 at 15:38

1 Comment

Tnank you a lot! This code much shorter what i already have!
0

Some simple code.

const
 data = [{ battery_charge: 0, battery_discharge: 0, consumption: 404, grid_backflow: 0, grid_consumption: 3, pv_generation: -7, time: "2019-02-25T00:00:00+00:00" }, { battery_charge: 0, battery_discharge: 0, consumption: 404, grid_backflow: 0, grid_consumption: 3, pv_generation: -7, time: "2019-02-25T10:00:00+00:00" }],
 result = Object.values(data.reduce((r, { time, ...o }) => {
 Object
 .entries(o)
 .forEach(([name, value]) => (r[name] ??= { name, data: [] }).data.push({ time, value }));
 return r;
 }, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

answered Nov 17, 2021 at 18:40

3 Comments

Thanks for your short code example. Just question: what does this operator mean: ??=. in .forEach line. I can't find this operator on internet
it is the logical nullish assignment ??= operator.
Thank you! Will go to documentation!)
-1

var keys = {
 battery_charge: "Battery Charge",
 battery_discharge: "Battery discharge",
 consumption: "Consumption",
 grid_backflow: "Grid backflow",
 grid_consumption: "Grid consumption",
 pv_generation: "Pv generation",
 };
 var data = [
 {
 battery_charge: 0,
 battery_discharge: 0,
 consumption: 404,
 grid_backflow: 0,
 grid_consumption: 3,
 pv_generation: -7,
 time: "2019-02-25T00:00:00+00:00",
 },
 {
 battery_charge: 0,
 battery_discharge: 0,
 consumption: 404,
 grid_backflow: 0,
 grid_consumption: 3,
 pv_generation: -7,
 time: "2019-02-25T10:00:00+00:00",
 },
 ];
 var formatted = [];
 data.map((k) => {
 Object.keys(keys).map((x) => {
 if (!formatted.some((f) => f.key == x)) {
 formatted.push({
 title: keys[x],
 key: x,
 data: [],
 });
 }
 if (
 formatted.some(
 (y) => y.data.some((f) => f.time == k.time) && y.key == x
 )
 ) {
 var index = formatted.findIndex(
 (y) => y.data.some((f) => f.time == k.time) && y.key == x
 );
 var dataIndex = formatted[index].data.findIndex(
 (f) => f.time == k.time
 );
 formatted[index].data[dataIndex].value += k[x];
 } else {
 var index = formatted.findIndex((k) => k.key == x);
 formatted[index].data.push({ value: k[x], time: k.time });
 }
 });
 });
 console.log(formatted);

answered Nov 17, 2021 at 18:28

1 Comment

Thank you a lot!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.