I have parse csv file into javascript object.
array[1] = ["2011-07-18", "373.800"]
array[2] = ["2011-07-19", "372.300"]
How can I make a new array that only consist?
["373.800", "372.300"]
Try this.
var items = [];
for( var i = 0; i < array.length; i++ ){
items.push( array[i][1] );
}
In newer browser (I mean Chrome/Firefox 3+), you can achieve this easily by Array.map https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/map. You could refer to Compatibility section to implement Array.map yourself for older browser compatibility.
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
res[i] = fun.call(thisp, t[i], i, t);
}
return res;
};
}
var array = [];
array[0] = ["2011-07-18", "373.800"];
array[1] = ["2011-07-19", "372.300"];
var result = array.map(function(ary) {
return ary[1];
});
alert(result);