1

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

2
  • Is there any error? Commented Oct 21, 2016 at 5:32
  • Did you check what you are getting in console Commented Oct 21, 2016 at 5:32

2 Answers 2

1

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])
Sign up to request clarification or add additional context in comments.

3 Comments

what is the use of arrayTemp ?
Nothing really. That's just vestigial from OP's code. I'll clean that out
what about ['jobs', 'Jobs Available'] where do you add it?
1

Use the proper each syntax, right now your arrayTemp is defined globally so you keep adding element to it

try the following:

arrayToDataTable = [['jobs', 'Jobs Available']];

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

demo:https://jsfiddle.net/uqxm0kf3/

or:

 arrayToDataTable = [['jobs', 'Jobs Available']];
        
        $('.skills').each(function(){
    
         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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.