[英]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.