create array variable

↘锁芯ラ 提交于 2019-12-01 11:54:05

问题


I wanted to create this kind of output

var s1 = [['Sony',7],['Samsung',5],['LG',8]];

so that I could use it to pass on my graph as a variable

out from the resutl of my ajax

success: function(data){

    //code to extract the data value here

    var s1= need to create the data here

    $.jqplot('chart',[s1],{ blah blah blah

}

"data" in the success function returns this table layout

<table id="tblResult">
    <tr class="tblRows">
        <td class="clsPhone">Sony</td><td class="clsRating">7</td>
    </tr>
    <tr class="tblRows">
        <td class="clsPhone">Samsung</td><td class="clsRating">5</td>
    </tr>
    <tr class="tblRows">
        <td class="clsPhone">LG</td><td class="clsRating">8</td>
    </tr>
</table>

can you please help me create the logic for this?

Thanks in advance

EDIT: I'm looking for a solution something like the following:

var s1;
$(".tblRows").each(function(){
    // here I don't know exactly on what to do
    //s1.push($(".clsPhone").text(),$(".clsRating").text()));
});
// all I wanted is to make the resul s1=[['Sony',7],['Samsung',5],['LG',8]];

because jqplot requires this kind of parameter

s1=[['Sony',7],['Samsung',5],['LG',8]];
$.jqplot('chart',[s1],{
        renderer:$.jqplot.PieRenderer,
        rendererOptions:{
            showDataLabels:true,
            dataLabelThreshold:1
        }
    }
});

so I'm looking for a way to create a the value for the variable s1 out from the data could this be possible?


回答1:


var s1 = [];
$(".tblRows").each(function(){
    // create a temp array for this row
    var row = [];
    // add the phone and rating as array elements
    row.push($(this).find('.clsPhone').text());
    row.push($(this).find('.clsRating').text());
    // add the temp array to the main array
    s1.push(row);
});



回答2:


You can do something like this:

var row = [];
$(".tblRows").each(function () {
    row.push([$(this).find('.clsPhone').text(),
              $(this).find('.clsRating').text()]);
});

$.jqplot('chart', [row], {
    //... 
});


来源:https://stackoverflow.com/questions/5550911/create-array-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!