1

I got this one :

const info = [["Month", " \"Average\"", " \"2005\"", " \"2006\"", " \"2007\"", " \"2008\"", " \"2009\"", " \"2010\"", " \"2011\"", " \"2012\"", " \"2013\"", " \"2014\"", " \"2015\""]
              ["May", "  0.1", "  0", "  0", " 1", " 1", " 0", " 0", " 0", " 2", " 0", "  0", "  0  "]
              ["Jun", "  0.5", "  2", "  1", " 1", " 0", " 0", " 1", " 1", " 2", " 2", "  0", "  1"]
              ["Jul", "  0.7", "  5", "  1", " 1", " 2", " 0", " 1", " 3", " 0", " 2", "  2", "  1"]
              ["Aug", "  2.3", "  6", "  3", " 2", " 4", " 4", " 4", " 7", " 8", " 2", "  2", "  3"]
              ["Sep", "  3.5", "  6", "  4", " 7", " 4", " 2", " 8", " 5", " 2", " 5", "  2", "  5"]
              ["Oct", "  2.0", "  8", "  0", " 1", " 3", " 2", " 5", " 1", " 5", " 2", "  3", "  0"]
             

and I would like to get the first one of those like :

["May","Jun","Jul","Aug"...] like this without the first one

so I tried to use slice. so the results was like this:

I tried console.log(info.slice(1, 10)[0]);

this and the result is like :

["May", " 0.1", " 0", " 0", " 1", " 1", " 0", " 0", " 0", " 2", " 0", " 0", " 0 "] so How can I get the first one of many of arrays?? like this:

["May","Jun","Jul","Aug"...]

Somi
  • 141
  • 6

2 Answers2

3

var myData = [["Jan", 34.1, 43.1, 55.2], ["feb", 5.3, 23.6, 40.9], ["mar", 43.5, 77.8, 22.4]],
    arrayTitle = myData.map(function(x) {
        return x[0];
    });

console.log(arrayTitle);
Bharat
  • 1,192
  • 7
  • 14
2

I'm assuming you want to return the first item in each array item of your array (arr). You can use Array.prototype.map like below:

var arr = [
  ['January', 'someValue', 1],
  ['February', 'someValue', 2],
  ['March', 'someValue', 3],
  ['April', 'someValue', 4],
  ['May', 'someValue', 5],
  ['June', 'someValue', 6],
  ['July', 'someValue', 7],
  ['August', 'someValue', 8],
  ['September', 'someValue', 9],
  ['October', 'someValue', 10],
  ['November', 'someValue', 11],
  ['December', 'someValue', 12]
];

var result = arr.map(i => i[0]); // same as `arr.map(function(i) { return i[0]; });`

console.log(result);

You might also need to use Array.prototype.slice to remove the first element array since you have a some kind of a header in your example.

var arr = [
  ['Month', 'Header 1', 'Header 2'],
  ['January', 'someValue', 1],
  ['February', 'someValue', 2],
  ['March', 'someValue', 3],
  ['April', 'someValue', 4],
  ['May', 'someValue', 5],
  ['June', 'someValue', 6],
  ['July', 'someValue', 7],
  ['August', 'someValue', 8],
  ['September', 'someValue', 9],
  ['October', 'someValue', 10],
  ['November', 'someValue', 11],
  ['December', 'someValue', 12]
];

var result = arr.slice(1).map(i => i[0]);

console.log(result);
xGeo
  • 2,149
  • 2
  • 18
  • 39