开发者

Extracting elements from a two dimensional array

开发者 https://www.devze.com 2023-03-01 03:16 出处:网络
I am geting the data in this format . {\"items\":[[\"Jan 1, 2010\",1],[\"Jan 2, 2010\",2]]} Is it possible to store only the dates from the array ??

I am geting the data in this format .

{"items":[["Jan 1, 2010",1],["Jan 2, 2010",2]]}

Is it possible to store only the dates from the array ?? Please help

when i am doing this way , its storing the whole date and numeric

var dateData[]

for (var i = 0; i < data.items.length; i++)  
{
 dateData.push(i);
}

I am 开发者_运维技巧using only prototype.js and pure javascript . Please help . Thank you very much


Another functional way to write the same,

var dateData = data.items.map(function(item) {
    return item[0];
});


var dateData = [];
var priceData = [];

for( var i = 0; i < data.items.length; i++ ){
  dateData.push( data.items[i][0] );
  priceData.push( data.items[i][1] );
}


var dateData = [];

data =  {"items":[["Jan 1, 2010",1],["Jan 2, 2010",2]]};

for (var i = 0; i < data.items.length; i++)
{
 dateData.push(data.items[i][0]);
}


No prototype.js required, just pure JS here:

var obj = {"items":[["Jan 1, 2010",1],["Jan 2, 2010",2]]};
var dateData = [];
for (var i in obj.items) {
    if (obj.items.hasOwnProperty(i)) {
        dateData.push(obj.items[i][0]);
    }
}
0

精彩评论

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