简体   繁体   中英

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, eg:

  ["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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM