Home  >  Q&A  >  body text

Merge two arrays in the same row JQUERY

I have a problem that I cannot merge two arrays in the same row, I now have two arrays and then I want to merge them because they have the same length

Here are my two arrays that look like

I would like to have an output like this by combining but this is what I have tried

const ar1 = $('input[class=namecheckbox]').map((i, el) => ({id: el.id.slice(2)})).get();
const ar2 = $('.quantity_input').map((i, el) => ({quantity: el.value})).get();

const merge= $('input[class=namecheckbox]').map((i, el) => ({id: el.id.slice(2),quantity: el.value})).get();

console.log(ar1);
console.log(ar2);
console.log(merge);

Does anyone know how to solve my problem? Thanks in advance

P粉610028841P粉610028841181 days ago326

reply all(1)I'll reply

  • P粉044526217

    P粉0445262172024-04-04 10:35:25

    So you get the wrong value, const merge= $('input[class=namecheckbox]')... Because ar2 gets the data from that selection, $('.quantity_input')...< /p>

    This also works,

    var merge = [];
    for(var i = 0; i < ar1.length; i++){
      let obj = {id: ar1[i]['id'], quantity: ar2[i]['quantity']};
      merge.push(obj);
    }

    reply
    0
  • Cancelreply