jQuery 创建数组变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5550911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:21:33  来源:igfitidea点击:

create array variable

jqueryvariablesmultidimensional-arrayjqplot

提问by Carls Jr.

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

从我的 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

因为 jqplot 需要这种参数

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?

所以我正在寻找一种方法来从数据中创建变量 s1 的值,这可能吗?

回答by Adam Hopkinson

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);
});

回答by Arsen Khachaturyan

You can do something like this:

你可以这样做:

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

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