开发者

getting key and value in two separate arrays

Hi I need to get the month ans value in two separate arrays one for key month and one for key value separately, for below mention data

var data = [{month: "JUL", value:"5"},
            {month: "AUG", value:"7"},
            {month: "SEP", value:"9"},
            {month: "OCT", value:"11"},
            {month: "NOV", value:"13"},    
            {month: "DEC", value:"15"},
            {month: "JAN", value:"17"},
            {month: "FEB", value:"19"},
            {month: "MAR", value:"21"},
            {month: "APR", value:"23"},
            {month: "MAY", value:"25"},
            {month: "JUN", value:"27"}];

I am trying to do in the below mentioned way

var output = [];
for(var key in response)
    for(var value in response[key])
      {
    output.push(response[key][value]);开发者_如何学JAVA
     }
    alert("output ---------------------->"+output);

Kindly help ...


Your initial question says you want to get the values into two separate arrays, then in your code you've only declared one 'output' array so I'm not entirely sure what you really want to do, but maybe something like this:

var months = [];
var values = [];

for(var i=0; i < data.length; i++){
    months.push(data[i]["month"]);
    values.push(data[i]["value"]);
}

Your initial declaration of 'data' was as an array of objects, where each object has two properties. You don't want to use the for(x in y) syntax for an array; use a traditional for() like in my example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜