简体   繁体   English

为什么将子数组推入另一个数组时JavaScript为什么要添加额外的方括号?

[英]Why is JavaScript adding extra square brackets when I push a sub array into another array?

I am working on a CodeWars kata that involves multidimensional arrays. 我正在研究涉及多维数组的CodeWars kata。 I have an array with three elements. 我有一个包含三个元素的数组。 Each element is an array with a name (string) and a sum (number). 每个元素都是一个具有名称(字符串)和总和(数字)的数组。 I am checking the numbers. 我正在检查数字。 If they exceed a certain amount, I want to remove the sub array with that number and put it at the end of the array. 如果它们超过一定数量,我想删除具有该数字的子数组并将其放在数组的末尾。 I used splice() and push(). 我使用了splice()和push()。

In a test case that had a sub array that had to be moved, the sub array was pushed to the end of the parent array. 在具有必须移动的子数组的测试用例中,该子数组被推到父数组的末尾。 But, it had an extra set of brackets. 但是,它还有一组额外的括号。 I was wondering why this happened. 我想知道为什么会这样。 Is JavaScript automatically adding the brackets when the push takes place because the parent array has only sub arrays as elements? 由于父数组仅将子数组作为元素,因此在推送时JavaScript是否自动添加括号?

Here is a link to some screenshots of the Chrome Dev Tools and one from CodeWars. 这里是一些Chrome开发工具屏幕截图的链接,以及CodeWars的屏幕截图的链接。 I combined them into one image. 我将它们合并为一张图像。 The sub array at index 2 in the pop out window shows the sub array at the end that was moved/pushed in the first screenshot. 弹出窗口中索引2处的子数组显示了在第一个屏幕快照中移动/推动的末尾的子数组。 The second screenshot shows the expanded view. 第二张屏幕截图显示了展开的视图。 The final screenshot I am attaching is the result shown in CodeWars where you can see the double brackets on the last sub array. 我要附加的最终屏幕截图是CodeWars中显示的结果,您可以在最后一个子数组上看到双括号。

I cannot get the image to upload. 我无法上传图片。 So, here is a link to it online. 因此,这里是在线链接。 https://sta.sh/01tp7y7zaayi https://sta.sh/01tp7y7zaayi

Here is the section of code where this is happening. 这是发生这种情况的代码部分。 I included the sort. 我包括了排序。 But, I don't think it is affecting anything. 但是,我认为它不会影响任何事情。

Note: I had a loop to go through the three elements to check them where the comment is located. 注意:我有一个循环遍历这三个元素来检查它们在注释中的位置。 But, I removed it because one shouldn't loop through an array and make changes to that array. 但是,我删除了它,因为不应循环遍历一个数组并对该数组进行更改。 I am coding a map() method for this to return a new array. 我正在为此编写一个map()方法以返回一个新数组。 But, I noticed that the double brackets were appearing. 但是,我注意到出现了双括号。 So, I didn't finish the map() part. 因此,我没有完成map()部分。 I am trying to figure out what is happening in that splice() / push() section first. 我试图首先弄清楚在splice()/ push()部分中发生了什么。

var arrNew = [["Ben", sumBen], ["Amy", sumAmy], ["Sam", sumSam]];

arrNew.sort(function (a,b) {
    if (a[1] < b[1]) return  1;
    if (a[1] > b[1]) return -1;
    if (a[0] > b[0]) return  1;
    if (a[0] < b[0]) return -1;
    return 0;
});

// If someone's score is over 21, move them to the back.
var forMoving = arrNew.splice(0, 1);
arrNew.push(forMoving);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM