0

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"]

3 Answers 3

1

Try this.

var items = [];
for( var i = 0; i < array.length; i++ ){
    items.push( array[i][1] );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! very simple and clean!
0

By creating a new array ?

<script>
    array = new Array();
    array[0] = ["2011-07-18", "373.800"];
    array[1] = ["2011-07-19", "372.300"];

    array2 = new Array();
    array2[0] = [array[0][1], array[1][1]];

    alert(array2[0]);
</script>

Comments

0

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);

Comments

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.