合并同一行中的两个数组 JQUERY

huangapple go评论66阅读模式
英文:

Merge two arrays in same row JQUERY

问题

I have an issue where I can't merge two arrays in the same row, I have two arrays now, then I want to combine them since they have the same length.

这是我的两个数组的样子:

合并同一行中的两个数组 JQUERY

而我希望通过合并它们来获得如下输出,但这是我尝试的方式:

合并同一行中的两个数组 JQUERY

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);

有谁知道如何解决我的问题吗?提前感谢!

英文:

I have an issue where I can't merge two arrays in same row, I have two arrays now , then I want to combine them since they have the same length

this is my two array looks like

合并同一行中的两个数组 JQUERY

and I want Output like this by combining this but this is what I tried by

合并同一行中的两个数组 JQUERY

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);

Can anyone know how to solve my problem? thanks in advance

答案1

得分: 1

以下是已翻译的部分:

你得到错误的值是因为这个,
const merge= $('input[class=namecheckbox]')...,因为 ar2 从这个选择中获取数据,$('.quantity_input')...

这也可以工作,

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

英文:

You are getting wrong value due to this,
const merge= $('input[class=namecheckbox]')... Because ar2 getting data from this selection, $('.quantity_input')...

This also work,

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

huangapple
  • 本文由 发表于 2023年5月15日 12:03:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定