JacobIRR
JacobIRR

Reputation: 8946

javascript get second item from arrays in 2D array

In python I could do something like:

seconds = [i[1] for i in my_giant_2d_matrix]

How can I do this in JS if my data structure is, e.g.:

  ["2017-08-01 17:05:57.27-08", 0.9685023945717632],
  ["2017-08-01 17:05:57.27-08", 1.8976437986779295],
  ["2017-08-01 17:05:57.27-08", 7.834808973859426],
  ["2017-08-01 17:05:57.27-08", 9.24775164641563],
  ["2017-08-01 17:05:57.27-08", 9.736525537982486],
  ["2017-08-01 17:05:57.27-08", 2.923580004727238],
  ["2017-08-01 17:05:57.27-08", 6.727439970979467],
  ["2017-08-01 17:05:57.27-08", 7.018190596386605],
  ["2017-08-01 17:05:57.27-08", 0.9126885492079035],
  ["2017-08-01 17:05:57.27-08", 10.42473448771042],
  ["2017-08-01 17:05:57.27-08", 8.150613247753153],
  ["2017-08-01 17:05:57.27-08", 7.603327591939451]...

I want the floats for all these (second value). Thanks.

Upvotes: 0

Views: 1313

Answers (2)

Andrii Starusiev
Andrii Starusiev

Reputation: 7764

seconds = array.map(el => el[1])

Upvotes: 0

akuiper
akuiper

Reputation: 214957

You can use map:

var matrix = [["2017-08-01 17:05:57.27-08", 0.9685023945717632],
  ["2017-08-01 17:05:57.27-08", 1.8976437986779295],
  ["2017-08-01 17:05:57.27-08", 7.834808973859426],
  ["2017-08-01 17:05:57.27-08", 9.24775164641563],
  ["2017-08-01 17:05:57.27-08", 9.736525537982486],
  ["2017-08-01 17:05:57.27-08", 2.923580004727238],
  ["2017-08-01 17:05:57.27-08", 6.727439970979467],
  ["2017-08-01 17:05:57.27-08", 7.018190596386605],
  ["2017-08-01 17:05:57.27-08", 0.9126885492079035],
  ["2017-08-01 17:05:57.27-08", 10.42473448771042],
  ["2017-08-01 17:05:57.27-08", 8.150613247753153],
  ["2017-08-01 17:05:57.27-08", 7.603327591939451]];
  

console.log(
  matrix.map(x => x[1])
)

Upvotes: 4

Related Questions