How to append value to an array keeping old value intact in Javascript? - Stack Overflow

Google visualization API requires arrays like this:var data = google.visualization.arrayToDataTable([[&

Google visualization API requires arrays like this:

var data = google.visualization.arrayToDataTable([
         ['Element', 'Density', { role: 'style' }],
         ['Copper', 8.94, '#b87333'],            // RGB value
         ['Copper', 10.49, 'silver'],            // English color name
         ['Gold', 19.30, 'gold'],
         ['Gold', 21.45, 'color: #e5e4e2' ], // CSS-style declaration
      ]);

I have separately array of columns

var columns = ['Copper', 'Copper', 'Gold', 'Gold'];

for values

var values = [8.94, 10.49, 19.30, 21.45];

and for colors

var styles = ['#b87333', 'silver', 'gold', 'color: #e5e4e2'];

Am I really in need to write multilevel loop with multiple conditions to build this array? Or there are simpler ways?

I can't push because arrays should be intact.

Google visualization API requires arrays like this:

var data = google.visualization.arrayToDataTable([
         ['Element', 'Density', { role: 'style' }],
         ['Copper', 8.94, '#b87333'],            // RGB value
         ['Copper', 10.49, 'silver'],            // English color name
         ['Gold', 19.30, 'gold'],
         ['Gold', 21.45, 'color: #e5e4e2' ], // CSS-style declaration
      ]);

I have separately array of columns

var columns = ['Copper', 'Copper', 'Gold', 'Gold'];

for values

var values = [8.94, 10.49, 19.30, 21.45];

and for colors

var styles = ['#b87333', 'silver', 'gold', 'color: #e5e4e2'];

Am I really in need to write multilevel loop with multiple conditions to build this array? Or there are simpler ways?

I can't push because arrays should be intact.

Share Improve this question asked Mar 31, 2017 at 15:03 Suzan CiocSuzan Cioc 30.2k64 gold badges221 silver badges390 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 6

Using Array#map.

var columns = ['Copper', 'Copper', 'Gold', 'Gold'],
    values = [8.94, 10.49, 19.30, 21.45],
    styles = ['#b87333', 'silver', 'gold', 'color: #e5e4e2'],
    res = columns.map((v,i) => [v, values[i], styles[i]]),
    result = [['Element', 'Density', { role: 'style' }], ...res];
    console.log(result);

Sounds like you just need to immutably copy the array and then do whatever you want with it. Try this:

// ES6
var myArr = [1, 2, 3]
var newArr = [ ...myArr ]

// ES5
var myArr = [1, 2, 3];
var newArr = [].concat(myArr);

You can map them together, here is a working fiddle

var columns = ['Copper', 'Copper', 'Gold', 'Gold'];
var values = [8.94, 10.49, 19.30, 21.45];
var styles = ['#b87333', 'silver', 'gold', 'color: #e5e4e2'];

var newArr = columns.map((item, index) => {return [columns[index], values[index], styles[index]]})

console.log(newArr) //=> ['Copper', 8.94, '#b87333'],
                    //['Copper', 10.49, 'silver'],
                    //['Gold', 19.30, 'gold'],
                    //['Gold', 21.45, 'color: #e5e4e2' ],

ES6 solution using reduce function

var columns = ['Copper', 'Copper', 'Gold', 'Gold'];
var values = [8.94, 10.49, 19.30, 21.45];
var styles = ['#b87333', 'silver', 'gold', 'color: #e5e4e2'];

// Reduce all arrays to one vlaue
var result  = columns.reduce((acc, init, i) => {
    acc.push([init, values[i], styles[i]])
    return acc
}, [])


var out = [
         ['Element', 'Density', { role: 'style' }],
         ...result
      ]

console.log(out)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744301777a4567536.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信