只有数组中的最后一个元素在每次循环中被连接。

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

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 = [
    [&#39;x&#39;, &#39;y&#39;, &#39;z&#39;],
    [&#39;d&#39;, &#39;d&#39;, &#39;d&#39;],
    [&#39;f&#39;, &#39;y&#39;, &#39;s&#39;]
];

let finalOutput = [];
for (let i = 1; i &lt;= listA.length - 1; i++) {
    let dataIndx = 0;
    for (let item of listB) {
        if (item[1] !== &#39;d&#39;) {
            item[1] = listA[i][dataIndx];
            dataIndx++;
        }
    }
    finalOutput = finalOutput.concat(listB);
}

console.log(&#39;Undesired output:&#39;, finalOutput);

<!-- end snippet -->

Currrent undesired output:

[
    [&#39;x&#39;, &#39;11&#39;, &#39;z&#39;], [&#39;d&#39;, &#39;d&#39;, &#39;d&#39;], [&#39;f&#39;, &#39;12&#39;, &#39;s&#39;],
    [&#39;x&#39;, &#39;11&#39;, &#39;z&#39;], [&#39;d&#39;, &#39;d&#39;, &#39;d&#39;], [&#39;f&#39;, &#39;12&#39;, &#39;s&#39;],
    [&#39;x&#39;, &#39;11&#39;, &#39;z&#39;], [&#39;d&#39;, &#39;d&#39;, &#39;d&#39;], [&#39;f&#39;, &#39;12&#39;, &#39;s&#39;],
]

Expected finalOutput value to be

[
    [&#39;x&#39;, &#39;7&#39;,  &#39;z&#39;], [&#39;d&#39;, &#39;d&#39;, &#39;d&#39;], [&#39;f&#39;, &#39;8&#39;,  &#39;s&#39;],
    [&#39;x&#39;, &#39;4&#39;,  &#39;z&#39;], [&#39;d&#39;, &#39;d&#39;, &#39;d&#39;], [&#39;f&#39;, &#39;5&#39;,  &#39;s&#39;],
    [&#39;x&#39;, &#39;11&#39;, &#39;z&#39;], [&#39;d&#39;, &#39;d&#39;, &#39;d&#39;], [&#39;f&#39;, &#39;12&#39;, &#39;s&#39;],
]

答案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= [[&#39;x&#39;,&#39;y&#39;,&#39;z&#39;],[&#39;d&#39;,&#39;d&#39;,&#39;d&#39;],[&#39;f&#39;,&#39;y&#39;,&#39;s&#39;]]; 

let finalOutput = [];
for (let i = 1; i &lt;= listA.length - 1; i++) {
  let dataIndx = 0;
  let listBB = listB.map(x =&gt; ([...x])); // &quot;cloning an array of arrays&quot;
  for(let item of listBB){
  
    if (item[1] !== &#39;d&#39;) {
         
         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 -->

huangapple
  • 本文由 发表于 2020年1月6日 22:37:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613949.html
匿名

发表评论

匿名网友

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

确定