英文:
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.
这是我的两个数组的样子:
而我希望通过合并它们来获得如下输出,但这是我尝试的方式:
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
and I want Output like this by combining this but this is what I tried by
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 < ar1.length; i++){
let obj = {id: ar1[i]['id'], quantity: ar2[i]['quantity']};
merge.push(obj);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论