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

293
Visualizações
How can I create an array from names and values of checkboxes in Javascript?

Imagine I have an array of selected checkboxes. Each has a value and name that corresponds with a filter.

<input class="filter-item--checkbox" type="checkbox" name="colors" value="blue">
<input class="filter-item--checkbox" type="checkbox" name="colors" value="orange">
<input class="filter-item--checkbox" type="checkbox" name="items" value="chair">
<input class="filter-item--checkbox" type="checkbox" name="items" value="bed">
<input class="filter-item--checkbox" type="checkbox" name="material" value="plastic">
<input class="filter-item--checkbox" type="checkbox" name="material" value="wood">

How can I sort these into one array, like below?

const filters = [
 'colors': [ 'blue', 'orange'],
 'items': ['chair', 'bed'],
 'material': ['plastic', 'wood'],
]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could get the elements and build an object of arrays.

const result = {};

for (const { name, value } of document.getElementsByClassName('filter-item--checkbox')) {
    (result[name] ??= []).push(value);
}

console.log(result);
<input class="filter-item--checkbox" type="checkbox" name="colors" value="blue">
<input class="filter-item--checkbox" type="checkbox" name="colors" value="orange">
<input class="filter-item--checkbox" type="checkbox" name="items" value="chair">
<input class="filter-item--checkbox" type="checkbox" name="items" value="bed">
<input class="filter-item--checkbox" type="checkbox" name="material" value="plastic">
<input class="filter-item--checkbox" type="checkbox" name="material" value="wood">

about 4 years ago · Juan Pablo Isaza Relatório

0

For the OP's use case, where it was said that a solution should target just checked checkbox controls one could/should utilize the FormData Web-API and its entries method together with a reduce task. Passing a form reference directly into the FormData constructor ensures only active control data to be part of the form data object. Thus the name/key and value of an unchecked checkbox control gets not reflected by the form data object.

const formData = new FormData(document.forms[0]);
console.log(
  Array
    .from(
      formData.entries()
    )
    .reduce((result, [key, value]) => {

      const groupedValueList = (result[key] ??= []);
      groupedValueList.push(value);

      return result;

    }, {})
);
.as-console-wrapper { min-height: 85%!important; }
<form>
  <input type="checkbox" name="colors" value="blue" checked/>
  <input type="checkbox" name="colors" value="orange" checked/>
  <input type="checkbox" name="items" value="chair" checked/>
  <input type="checkbox" name="items" value="bed" checked/>
  <input type="checkbox" name="material" value="plastic" checked/>
  <input type="checkbox" name="material" value="wood" checked/>
</form>

In case of collecting all grouped checkbox values regardless of their checked state one just needs to change the above approach slightly too ...

console.log(
  Array
    .from(
      document.querySelectorAll('form [type="checkbox"]')
    )
    .reduce((result, { name:key, value }) => {

      const groupedValueList = (result[key] ??= []);
      groupedValueList.push(value);

      return result;

    }, {})
);
.as-console-wrapper { min-height: 85%!important; }
<form>
  <input type="checkbox" name="colors" value="blue" checked/>
  <input type="checkbox" name="colors" value="orange"/>
  <input type="checkbox" name="items" value="chair" checked/>
  <input type="checkbox" name="items" value="bed"/>
  <input type="checkbox" name="material" value="plastic" checked/>
  <input type="checkbox" name="material" value="wood"/>
</form>

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