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
3
  • 2
    What is expected output ? Commented Feb 12, 2019 at 5:04
  • Possible duplicate of Get array of object's keys Commented Feb 12, 2019 at 5:14
  • There isn't any JSON in the code you posted. JSON is a text representation of some data structure. All you have there is an object whose properties are arrays. No JSON. Commented Feb 12, 2019 at 7:35

3 Answers 3

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);

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

Comments

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);

Comments

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]

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.