0

I have Json Parse list which need to push each category list. example :

    listChartPeriods={"2018-05-04":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-11":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-18":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-25":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-06-01":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442]}
  var categoryData = [];
  var values = [];
  for(var i=0;i<listChartPeriods.length;i++){
      categoryData.push(listChartPeriods.slice(0,1)[0]); //here need to push each date  
      values.push(listChartPeriods[i])
   }

expected out put:

categoryData=["2018-05-04","2018-05-11","2018-05-18","2018-05-25","2018-06-01"]

 values=[21807210.5028442,21807210.5028442,21807210.5028442]//each category values
sameer
  • 447
  • 3
  • 12
  • 20
  • 2
    What is expected output ? – Code Maniac Feb 12 '19 at 05:04
  • Possible duplicate of [Get array of object's keys](https://stackoverflow.com/questions/8763125/get-array-of-objects-keys) – Code Maniac Feb 12 '19 at 05:14
  • There isn't any JSON in the code you posted. [JSON](http://json.org) is a text representation of some data structure. All you have there is an object whose properties are arrays. No JSON. – axiac Feb 12 '19 at 07:35

3 Answers3

3

Just use Object.keys to get the dates in the array.

const listChartPeriods={"2018-05-04":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-11":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-18":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-25":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-06-01":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442]}
var categoryData = Object.keys(listChartPeriods);
console.log(categoryData);
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
2

Below should get the job done for you. A for in loop is your friend when it comes to working with objects.

var listChartPeriods={"2018-05-04":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-11":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-18":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-25":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-06-01":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442]}
var categoryData = [];

for(var char in listChartPeriods){
 for(var i = 0; i < listChartPeriods[char].length; i++){
  categoryData.push(listChartPeriods[char][i]);
 }
}
console.log(categoryData);

EDIT: Just read your updated question and you are only wanting the key names. You can also do this with a for in loop.

for(var char in listChartPeriods){
 categoryData.push(char)
}
console.log(categoryData);
Spangle
  • 762
  • 1
  • 5
  • 14
0

Following the solution:

 for (let date in listChartPeriods){
    categoryData.push(date);
    let [first] = listChartPeriods[date];
    values.push(first);
 }

categoryData = ["2018-05-04", "2018-05-11", "2018-05-18", "2018-05-25", "2018-06-01"]

values = [21807210.5028442, 21807210.5028442, 21807210.5028442, 21807210.5028442, 21807210.5028442]

Linto
  • 31
  • 1
  • 6