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

151
Visualizações
jquery .each function using 2 selectors and adding value from selectors as key value pair to an array

I am using the jquery .each function utilizing 2 selectors. When pushing the values to an array it adds them as each selector value as its own entry into the array.

in example array(100,casement,200,glider)

what I am trying to do is add them in key value pair in an array such as

array(100=>casement, 200=>glider)

What do I need to change to make this happen?

 <input name="windowNum"><input name="windowList">
 <input name="windowNum"><input name="windowList">
 <input name="windowNum"><input name="windowList">
 <input name="windowNum"><input name="windowList">

<script>
function getFields(){
  $("input[name='windowNum'], input[name='windowList']").each(function() {

      var winList = $(this).val();

      windowList.push(winList);

      //I want an array output like this from the 2 fields
      //windowList.push(input[name='windowNum'] : input[name='windowList'])
  });
}
</script>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

It is working on my end. Try to test it into this code snippet. Or I probably did not understand your expected output.

var windowList = {};

function getFields(){
  $("input[name='windowNum']").each(function() {
      var key = $(this).val();        
      windowList[key] = $(this).next().val(); 
  });
  
  console.log(windowList);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input name="windowNum"><input name="windowList"><br>
 <input name="windowNum"><input name="windowList"><br>
 <input name="windowNum"><input name="windowList"><br>
 <input name="windowNum"><input name="windowList">
 
 <button type="button" onclick="getFields()">Test</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

Use map instead of each

let arr = [100, casement, 200, glider];

arr = arr.map((v, i) = > {
    if (i % 2 === 1 || !arr[i + 1]) return null;
    return `${v}=>${arr[i + 1]}`;
}).filter(Boolean);
about 4 years ago · Juan Pablo Isaza Relatório

0

I solved it. Here is what I did.

$("input[name='windowNum'], input[name='windowList']").each(function() {

    var winList = $(this).val();

    windowList_x.push(winList);
});

for (let i = 0; i < windowList_x.length; i+=2) {

var wNum = windowList_x[i];
var wName = windowList_x[i + 1];

windowList.push({number: wNum, window: wName});

}

It outputs

Array
0: Object { number: "101", window: "test a" }
1: Object { number: "102", window: "test b" }
2: Object { number: "103", window: "test c" }
3: Object { number: "104", window: "test d" }
length: 4
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