1

Please find my json array, I want to form array of data in below format:

[Sunday.a_count.value, Monday.a_count.value, Tuesday.a_count.value, Wednesday.a_count.value] - This format output will be [0, 2, 2, 3] -

Eventually, these values come from Sunday a_count, Monday a_count, Tuesday a_count and Wednesday a_count.

I'm not able to form the data in above format using below json in javascript, if someone can help to achieve this it will be really helpful.

Thanks!

[ {
  "day" : "Sunday",
  "a_count" : 0,
  "b_count" : 2,
  "c_count" : 0,
  "d_count" : 0
}, {
  "day" : "Monday",
  "a_count" : 2,
  "b_count" : 0,
  "c_count" : 0,
  "d_count" : 0
}, {
  "day" : "Tuesday",
  "a_count" : 2,
  "b_count" : 0,
  "c_count" : 0,
  "d_count" : 0
}, {
  "day" : "Wednesday",
  "a_count" : 3,
  "b_count" : 0,
  "c_count" : 0,
  "d_count" : 0
} ]

1 Answer 1

2

As I understood, you want to get value from a_count, you can do it with .map, like so

var data = [ {
  "day" : "Sunday",
  "a_count" : 0,
  "b_count" : 2,
  "c_count" : 0,
  "d_count" : 0
}, {
  "day" : "Monday",
  "a_count" : 2,
  "b_count" : 0,
  "c_count" : 0,
  "d_count" : 0
}, {
  "day" : "Tuesday",
  "a_count" : 2,
  "b_count" : 0,
  "c_count" : 0,
  "d_count" : 0
}, {
  "day" : "Wednesday",
  "a_count" : 3,
  "b_count" : 0,
  "c_count" : 0,
  "d_count" : 0
} ];

var res = data.map(function (el) {
  return el.a_count;
});

console.log(res);

1
  • I got it. Thanks for the answer! Appreciate your help Commented Sep 11, 2015 at 5:59

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.