javascript - naming variables from loop jquery -


this follow question https://stackoverflow.com/questions/34981100/jquery-loop-to-generate-variables

so i'm trying make variables in newly created arrays in function. have call each newly created variable chart.

var digits = [2012, 01, 5, 88, 2012, 01, 6, 90, 2012, 05, 9, 130]; size = 3; data = []  while (digits.length > 0) data.push(digits.splice(0, size)); var mysize = data.length; (i = 0; < data.length; i++) {   x = 0   console.log("this set " + i)   console.log('this data array ' + + '\n ' + data[i])   (j = 0; j < 3; j++)   console.log(data[i][j]) 

my second console.log statement prints out arrays in sets of four. need name each of arrays, , need able call each individual array in chart, in separate function.

right each array named line

console.log('this data array ' + + '\n ' + data[i]) 

with no variable set line.

the end result should my1 = [2012,1,5,88] my2 = [2012, 1,6, 90] my3 = [2012, 5, 9, 130] , since data array scale wont able know how many new variable arrays need make. that's why code should auto generate new array variable names, , break original data sets of four.

then have pass each newly created variable separately chart.

what need object, not array. objects have keys can use identifier.

;(function(){    'use strict';      const chunk_size = 3;  let digits       = [2012, 1, 5, 88, 2012, 1, 6, 90, 2012, 5, 9, 130];  let names        = ['foo', 'bar', 'baz'];  let data         = {};    for(let = 0; digits.length; i++)    data[names[i]] = digits.splice(0, chunk_size);    console.log(data.foo);  console.log(data.bar);  console.log(data.baz);        }());

also, notice of numbers start 0. js treat them octal instead of decimal. either remove 0 become decimals or if want preserve it, make them strings instead.


Comments