Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda