3

I have this object:

var json = {
  "alex" : [
    {'count' : 1, 'date': 2},
    {'count' : 2, 'date': 2},
  ],
  "alex" : [
    {'count' : 10, 'date': '1'},
    {'count' : 20, 'date': '10'},
  ],
};

How do I convert it to:

var arr = [
  {
    name: 'alex',
    data: [[10,1],[20,2]]
  },
  {
    name: 'bob',
    data: [[10,1],[20,2]]
  }
]
2
  • 1
    That is not JSON. It is a javascript object literal. And an invalid one at that, as you can't have the same property listed twice. That being said, have you made any effort at all to achieve this? Commented Nov 16, 2014 at 17:33
  • pass data from php to highcharts {type area and many series} so the way i think is convert php array to json i got format above and it works well Commented Nov 16, 2014 at 21:07

1 Answer 1

7
var json = {
  "alex" : [
    {'count' : 1, 'date': 2},
    {'count' : 2, 'date': 2},
  ],
  "bob" : [
    {'count' : 10, 'date': '1'},
    {'count' : 20, 'date': '10'},
  ],
};


var res = Object.keys(json).map(function (el) {
  return {
    name: el,
    data: json[el].map(function (e) {
      return [e.count, e.date]    
    })
  }  
})

console.log(res);
Sign up to request clarification or add additional context in comments.

Comments

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.