[英]Loop through array and evaluate expression
我是 JS 新手。 我有以下语法,我需要遍历数组。
{
text: newcol[4],
action: function (e, dt, node, config) {
dt.order([[4, "desc"]]).draw();
}
}
假设我有一个数组var nOrder = [4,5];
我希望生成以下语法 - 上面的语法重复两次(根据数组的长度)并取值 4 和 5。
buttons = [
{
text: newcol[4],
action: function (e, dt, node, config) {
dt.order([[4, "desc"]]).draw();
}
},
{
text: newcol[5],
action: function (e, dt, node, config) {
dt.order([[5, "desc"]]).draw();
}
}
];
这是你需要的吗? 我认为您不需要使用 JQuery。
function generateSyntax(array) { // The argument should be an array such as [4, 5]
var resultSyntax = ""; // Every time you loop through the array, add some syntax here.
resultSyntax += "buttons = [\n";
for (var i in array) {
resultSyntax += ` {
text: newcol[` + array[i] + `],
action: function (e, dt, node, config) {
dt.order([[` + array[i] + `, "desc"]]).draw();
}
},\n`; // Add the syntax, with the number in between brackets ("[" and "]")
}
resultSyntax += "];"
return resultSyntax;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.