英文:
Only last element from the array gets concatenated in every loop
问题
问题:在循环中,只有数组的最后一个元素被连接到finalOutput
。
当前不希望的输出:
[
['x', '11', 'z'], ['d', 'd', 'd'], ['f', '12', 's'],
['x', '11', 'z'], ['d', 'd', 'd'], ['f', '12', 's'],
['x', '11', 'z'], ['d', 'd', 'd'], ['f', '12', 's'],
]
期望的finalOutput
值为:
[
['x', '7', 'z'], ['d', 'd', 'd'], ['f', '8', 's'],
['x', '4', 'z'], ['d', 'd', 'd'], ['f', '5', 's'],
['x', '11', 'z'], ['d', 'd', 'd'], ['f', '12', 's'],
]
英文:
Issue: Only the last element of the array is getting concatenated in finalOutput
in a loop
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let listA = [
[1, 2],
[7, 8],
[4, 5],
[11, 12]
];
let listB = [
['x', 'y', 'z'],
['d', 'd', 'd'],
['f', 'y', 's']
];
let finalOutput = [];
for (let i = 1; i <= listA.length - 1; i++) {
let dataIndx = 0;
for (let item of listB) {
if (item[1] !== 'd') {
item[1] = listA[i][dataIndx];
dataIndx++;
}
}
finalOutput = finalOutput.concat(listB);
}
console.log('Undesired output:', finalOutput);
<!-- end snippet -->
Currrent undesired output:
[
['x', '11', 'z'], ['d', 'd', 'd'], ['f', '12', 's'],
['x', '11', 'z'], ['d', 'd', 'd'], ['f', '12', 's'],
['x', '11', 'z'], ['d', 'd', 'd'], ['f', '12', 's'],
]
Expected finalOutput
value to be
[
['x', '7', 'z'], ['d', 'd', 'd'], ['f', '8', 's'],
['x', '4', 'z'], ['d', 'd', 'd'], ['f', '5', 's'],
['x', '11', 'z'], ['d', 'd', 'd'], ['f', '12', 's'],
]
答案1
得分: 1
问题在于你多次迭代listB
并每次修改该列表(引用相同的数组)。由于数组是一个引用,最后一个将被捕获在你的结果集中。尝试克隆listB
而不是修改它:
let listA = [[1,2],[7,8],[4,5],[11,12]];
let listB = [['x','y','z'],['d','d','d'],['f','y','s']];
let finalOutput = [];
for (let i = 1; i <= listA.length - 1; i++) {
let dataIndx = 0;
let listBB = listB.map(x => ([...x])); // "克隆一个数组的数组"
for(let item of listBB){
if (item[1] !== 'd') {
item[1] = listA[i][dataIndx];
dataIndx++;
}
finalOutput = finalOutput.concat(listBB);
}
}
console.log(finalOutput)
注意:这只是你提供的代码的翻译,没有其他内容。
英文:
The problem here is that you iterate through listB
several times and modify that list every time (referene to the same array). Since array is a reference the last one will be caputered in your result set. Try to clone listB
instead:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let listA= [[1,2],[7,8],[4,5],[11,12]];
let listB= [['x','y','z'],['d','d','d'],['f','y','s']];
let finalOutput = [];
for (let i = 1; i <= listA.length - 1; i++) {
let dataIndx = 0;
let listBB = listB.map(x => ([...x])); // "cloning an array of arrays"
for(let item of listBB){
if (item[1] !== 'd') {
item[1] = listA[i][dataIndx];
dataIndx++;
}
finalOutput = finalOutput.concat(listBB);
}
}
console.log(finalOutput)
<!-- end snippet -->
答案2
得分: 1
你可以对第一个数组进行切片,并对嵌套数组使用 flatMap。
let listA = [[1, 2], [7, 8], [4, 5], [11, 12]],
listB = [['x', 'y', 'z'], ['d', 'd', 'd'], ['f', 'y', 's']],
result = listA
.slice(1)
.flatMap(([v]) => listB.map(([a, b, c]) => [a, b === 'd' ? b : v, c]));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
英文:
You could slice the first array and take a flatMap for the nested array.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let listA = [[1, 2], [7, 8], [4, 5], [11, 12]],
listB = [['x', 'y', 'z'], ['d', 'd', 'd'], ['f', 'y', 's']],
result = listA
.slice(1)
.flatMap(([v]) => listB.map(([a, b, c]) => [a, b === 'd' ? b : v, c]));
console.log(result);
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论