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

135
Visualizações
How can I work with in JavaScript a single HTML input field as an array?

After hours of searching and testing I finally have decided to post a question.

I need to be able to make something like this to work with a function.

var myCSV = [1,3,5,6];

Currently, I can grab the CSV from the database as that is how it is stored, myColumn = 1,3,5,6. I can get it to pass as an AJAX success response but it seems to want to add quotes. I then tried to add JSON encode to my PHP side of things and JSON Parse to the success call but still cannot get the function to work. The end goal is to select checkboxes based off the csv value.

This works

var FilterArray =  [1,3,5,6]; // Manually added these numbers as is for testing
$('#myForm').find('.checks').each(function () {
   $(this).prop("checked", ($.inArray(parseInt($(this).val()), FilterArray ) != -1));
});

After trying to much to get my AJAX success response to work in the FilterArray, I decided to just pass it to an input value and work with it. However, cannot figure out how to not treat it as a string when I pass it to the function. Here is what I have tried.

In my getCSV.php I have at the end this

json_encode($foundCSV);

In my AJAX Success

var FilterArray = JSON.parse(response);

I have also tried it without the json_encode and just sending it to an input value which does not add quotes.

So in summary, how can I take a csv e.g. 1,3,5,6 stored value and pass it to a function that works as shown above?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Assuming you have your string in a variable, called response, and its value is "1,3,5,6", you can translate it into an array of integers with this:

response.split(',').map(e => parseInt(e))

and you can then pass the result of this to your function. Sticking to your naming, the code should look like this:

var FilterArray = response.split(',').map(e => parseInt(e));

What the code does:

  • takes the response and split it by using the comma character as a delimiter
  • this will create an array of strings
  • for each string in that array, tries to parse it as an integer (an ID, i guess?)
about 4 years ago · Juan Pablo Isaza Relatório

0

Clarifying question: What does your $foundCSV look like on the PHP side, before you json-encode it? Is it an array or a string with comma-separated numbers? You might want to check with your developer tools/debugger what the Ajax response contains. It sounds like you have a string but expect an array.

If that's the case, you can create an array on the JS- or PHP-side. For the JS-side, consult @GregorioPalama's answer. Alternatively, you could do this on the PHP-side, which might be shorter:

$response = explode(',', $foundCSV);

and then work with json_encode($response).

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