EaBangalore EaBangalore - 1 month ago 4
Javascript Question

How to create an array dynamically in a specified manner

I have multiple values coming from a database, it increases as new record is added.

I'm making span like this:

<span style="display:none;" class="skills" data-skill="php programing">350</span>

<span style="display:none;" class="skills" data-skill="java programing">235</span>

<span style="display:none;" class="skills" data-skill="python programing">289</span>


I want to construct an array in this fashion

var data = google.visualization.arrayToDataTable([
['jobs', 'Jobs Available'],
['php programing', 350],
['java programing', 235],
['python programing', 289]
]);


so that I can be able to construct a google piechart dynamically

right now I am doing something like this

arrayToDataTable = [];
arrayTemp = [];
$.each('.skills',function(i){
var skill = $(this).data('skill');
var number = $(this).text();
arrayTemp.push(skill);
arrayTemp.push(number);
arrayToDataTable.push(arrayTemp);
});


**my question how can i make array in this fashion dynamically:**

[
['jobs', 'Jobs Available'],
['php programing', 350],
['java programing', 235],
['python programing', 289]
]


please help me thanks in advance

Answer

arrayToDataTable = [];

$.each('.skills',function(i){
 var skill = $(this).data('skill');
 var number = $(this).text();
  arrayToDataTable.push([skill, number]);
});

console.log(arrayToDataTable);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span style="display:none;" class="skills"  data-skill="php programing">350</span>

<span style="display:none;" class="skills"  data-skill="java programing">235</span>

<span style="display:none;" class="skills"  data-skill="python programing">289</span>

Have you tried:

arrayToDataTable.push([arrayTemp])