1

I have data like -

var data = [{"DefaultZone":[{"key":"stream0","value":100},
                        {"key":"stream1","value":50},
                        {"key":"stream2","value":10}
                       ]},
        {"Zone 1":[{"key":"stream0","value":120},
                  {"key":"stream1","value":55},
                  {"key":"stream2","value":15}
                  ]}
       ]        

and wanted to transform it like -

var data = [{"key": "stream0", "values":[{"x":"DefaultZone","y":100}, {"x":"Zone 1","y":120}]},
    {"key": "stream1", "values":[{"x":"DefaultZone","y":50}, {"x":"Zone 1","y":55}]},
    {"key": "stream2", "values":[{"x":"DefaultZone","y":10}, {"x":"Zone 1","y":15}]}
   ];

using JavaScript(ES6). Any help would be highly appreciated..

3
  • 1
    That's not JSON (JSON is a string format). I've edited your question to remove the references to "JSON", but you might like to edit it yourself to fix the syntax problems in your desired output (missing quotes, a missing comma). Commented Sep 5, 2016 at 2:33
  • What you show is a JavaScript object literal, not JSON. Is what you are showing actually what you have, or do you have actual JSON? Commented Sep 5, 2016 at 2:33
  • What have you already tried? Commented Sep 5, 2016 at 2:42

2 Answers 2

4

Here is the first way that came to mind:

var data = [{
  "DefaultZone": [
    { "key": "stream0", "value": 100 },
    { "key": "stream1", "value": 50 },
    { "key": "stream2", "value": 10 }]
}, { 
  "Zone 1": [
    { "key": "stream0", "value": 120 },
    { "key": "stream1", "value": 55 },
    { "key": "stream2", "value": 15 }]
}];

let working = data.reduce((p, c) => {
  let x = Object.keys(c)[0];
  c[x].forEach(v => {
    if (!p[v.key]) p[v.key] = [];
    p[v.key].push({ x: x, y: v.value });
  });
  return p;
}, {});

let output = Object.keys(working).map(v => ({ key: v, values: working[v] }));

console.log(output);

Further reading:

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

Comments

0

Although I like nnnnnn's answer, here is another one, using only the Object.keys() method:

var data = [{
    "DefaultZone": [{
        "key": "stream0",
        "value": 100
    }, {
        "key": "stream1",
        "value": 50
    }, {
        "key": "stream2",
        "value": 10
    }]
}, {
    "Zone 1": [{
        "key": "stream0",
        "value": 120
    }, {
        "key": "stream1",
        "value": 55
    }, {
        "key": "stream2",
        "value": 15
    }]
}]

final = {}
data.forEach(function(x) {
    Object.keys(x).forEach(function(y) {
        x[y].forEach(function(z) {
            if (!final[z['key']]) final[z['key']] = [];
            final[z['key']].push({
                'x': y,
                'y': z['value']
            })
        })
    })
})
answer = []
Object.keys(final).forEach(function(x) {
    answer.push({
        'key': x,
        values: final[x]
    })
})
console.log(answer)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.