Chartjs-Javascript 2D array getting each element

4๐Ÿ‘

โœ…

You can use map to do this, and shift the result in order to remove the first occurence.

var example = [
  ['Version', 'Number'],
  ['V1.0', 1],
  ['V2.0', 2]
];

var result = example.map(e => e[0])

console.log(result);

1๐Ÿ‘

From what I saw into your example the first pair of elements are the keys for your data, into your example will include them into your final arrays.

This example will generate to a dictionary with the keys Number and Version containing the corresponding values from your array.

var example = [['Version', 'Number'], [ 'V1.0', 1 ], [ 'V2.0', 2 ]];

function extract(items) {
  var keys = {},
      version = items[0][0],
      number = items[0][1];
  
  keys[version] = [];
  keys[number] = [];
   
  return items.slice(1).reduce(function(acc, item) {
    acc[version].push(item[0]);    
    acc[number].push(item[1]);
    
    return acc;
  }, keys);
}

var result = extract(example);
console.log(result);

From this point you can do something like:

var labels = result.Version;
var data = result.Number;

0๐Ÿ‘

This looks like what you are trying to achieve:

for(var i=0; i<example.length; i++){

  labels.push(example[i][0])
  data.push(example[i][1])
}

Leave a comment