Suppose I have the following array structure (say: for a plot with 3 lines):
let series1 = [1,2,3,4]
let series2 = [2,3,4,5]
let series3 = [2,6,6,7]
But I also need the following structure (say: for a jexcel table):
let data = [ [1,2,2], [2,3,6], [3,4,6], [4,5,7] ]
What would be the shortest way to create A from B (or vice versa)?
One possible way to create the desired output is using .map()
, .forEach()
combination.
Please find my example below:
let series1 = [1,2,3,4]; let series2 = [2,3,4,5]; let series3 = [2,6,6,7]; const arrays = ['series1', 'series2', 'series3']; const result = series1.map((c,i) => { const a = []; arrays.forEach(e => a.push(eval(e)[i])); return a; }); console.log(result);
With the provided solution the code expects all the series arrays to have the same length.
I hope that helps!
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.