Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
Loop through array and evaluate expression

I am new to JS. I have the following syntax which I need to loop through array.

  {
    text: newcol[4],
    action: function (e, dt, node, config) {
      dt.order([[4, "desc"]]).draw();
    }
  }

Let's say I have an array var nOrder = [4,5]; I want the following syntax to be generated - above syntax is getting repeated twice (as per length of array) and taking values 4 and 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();
    }
  }
];
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Is this what you need? I don't think you need to use 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;
}

If you want to get it as an actual array, you can use this (which is slower but safe):

function generateSyntax(array) { // The argument should be an array such as ["4", 5, "2"]
    var resultSyntax = ""; // Every time you loop through the array, add some syntax here.

    resultSyntax += "buttons = [\n";
    for (var i in array) {
        resultSyntax += `  {
    text: newcol[` + (parseInt(array[i] + "") != parseInt(array[i] + "") ? 0 : parseInt(array[i] + "")) + `],
    action: function (e, dt, node, config) {
      dt.order([[` + (parseInt(array[i] + "") != parseInt(array[i] + "") ? 0 : parseInt(array[i] + "")) + `, "desc"]]).draw();
    }
  },\n`; // Add the syntax, with the number in between brackets ("[" and "]")
    }
    resultSyntax += "];"
    return resultSyntax;
}

At the location of where you want to unpack the buttons object, use

// ... assign the newcol array and the dt.order function
var buttons;
eval(generateSyntax(myArrayToParse));
// do stuff with the resultant buttons variable.

This will change all non-number parameters to generateSyntax to 0, to make the eval call safer. As a result, generateSyntax will accept both number and string parameters.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda