Extracting elements from a two dimensional 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]);
}
}
精彩评论