Javascript 平坦数组理解代码
在我的另一个问题中,有人发布了一个非常酷的解决方案,介绍了如何将第n个嵌套数组展平为一个数组。因为我不想开始长时间的聊天,而且我还没有完全理解这段代码的作用,所以我想我应该问一下 因此,我的印象是,首先在本例中,数组的长度为2,然后在while循环中变为1。然后我们检查isJavascript 平坦数组理解代码,javascript,arrays,Javascript,Arrays,在我的另一个问题中,有人发布了一个非常酷的解决方案,介绍了如何将第n个嵌套数组展平为一个数组。因为我不想开始长时间的聊天,而且我还没有完全理解这段代码的作用,所以我想我应该问一下 因此,我的印象是,首先在本例中,数组的长度为2,然后在while循环中变为1。然后我们检查is数组[1],is是一个数组。所以我们继续。现在我有点困惑了。我相信我们再次调用了展平函数,这样我们就可以进入嵌套数组了,但我对推理还是有点模糊。然后我们取array[1]并对其进行切片,这里的切片不仅仅意味着得到整个array
数组[1]
,is是一个数组。所以我们继续。现在我有点困惑了。我相信我们再次调用了展平
函数,这样我们就可以进入嵌套数组了,但我对推理还是有点模糊。然后我们取array[1]
并对其进行切片,这里的切片不仅仅意味着得到整个array[l]
?因为slice()
没有参数,所以我们从第0个位置一直到最后
function flatten(array) {
var l = array.length, temp;
while (l--) {
if (Array.isArray(array[l])) {
flatten(array[l]);
temp = array[l].slice();
temp.unshift(1);
temp.unshift(l);
[].splice.apply(array, temp);
}
}
}
var array = [['1', '2', '3'], ['4', '5', ['6'], ['7', '8']]];
flatten(array);
console.log(array);
参考文献:
有关详细说明,请参见注释
function flatten(array) {
// put the length of the array in l
// and create a temp variable with nothing in it
var l = array.length, temp;
// while(l--) will iterate through
// each item in the array backwards
// this is just a little faster than doing
// for(var i=0; i<array.length; i++)
while (l--) {
// The current item in the while loop is array[l]
// (that's an L, by the way, not a one)
// so this is saying "if current item is an array...."
if (Array.isArray(array[l])) {
// we call the function again (recursion)
// if this is an array. eventually, one of
// the inner items will be something other
// than an array and the recursion will stop
flatten(array[l]);
// when you call slice() with no parameters
// all it does is create a copy of the entire array
// and then we store it in temp
temp = array[l].slice();
// adds the number 1 to the begining of the array
// after we unshift one more time below,
// this will become the second parameter for the
// splice() call below, which is the number of
// items to delete
temp.unshift(1);
// adds the current index to the begining of the array
// this will be the first argument passed to splice()
// below, the first argument for splice is the
// index to start splicing..
temp.unshift(l);
// apply()'s first argument is context, in this case
// passing "array" as the context means the action will
// be performed on the array variable,
// which is the original arrray..
// apply()'s second argumetn is an array, each item
// of the array is passed in order to the function
// as arguments
// So basically this is saying.. "remove the current
// index (which is an array) and replace it with each
// of the items that were in that array
[].splice.apply(array, temp);
}
}
}
函数展平(数组){
//将数组的长度用l表示
//并创建一个不包含任何内容的临时变量
var l=数组长度,温度;
//而(l--)将遍历
//数组中的每个项都向后
//这只是比做这个要快一点
//对于(var i=0;i,我假设您了解递归的基本知识。我将逐行介绍您
var l = array.length, temp;
声明l等于数组的长度,并声明temp
while (l--)
这会在循环迭代后递减l(与之前的--l相反)
这是检查数组中的第l个元素是否是另一个数组。这很重要,因为这意味着该元素不是平坦的
flatten(array[l]);
这就是它有趣的地方,函数递归地调用自己,这样它现在可以遍历子数组。如果子数组包含另一个数组,它可以继续深入。我相信这是头递归
temp = array[l].slice();
看起来有点奇怪,但这允许我们将数组提取到一个名为temp的新变量中
temp.unshift(1);
temp.unshift(l);
[].splice.apply(array, temp);
这也是一种非常简陋的编写方式,但基本上它将1和l作为temp数组中的第一个to元素,然后调用数组上的splice,使用temp作为参数。只有temp的前两个元素作为参数传递(我们刚才输入的两个),因此它基本上删除了子数组,并将其替换为展平版本。记住,我们正在向后迭代(l--)因此,在第一次迭代中,l将=3lol,我的错误,你是对的,数组中有2个项,我们在每次迭代之前,在第一次迭代中,l将实际=1,正如你最初所说的
,递减发生在循环之前,而不是之后。在第一次迭代时它将是1。当你这样做时--i它将在递减之后计算i
,这意味着循环将永远不会在第0个索引上迭代。如果这个答案有帮助,请点击复选标记,如果你喜欢,你也可以向上投票。希望我有帮助。不,数组长度是starts为3,循环的每次迭代都会使其递减。[].splice本身不做任何事情,您必须阅读apply方法。它的语法非常奇怪,我不确定最好的解释方法。抱歉。是的,它是一个2元素数组。我一直误读它。它还与apply函数有关。splice是一个在数组上工作的方法,因此[]是要在其上调用函数的空数组。
temp.unshift(1);
temp.unshift(l);
[].splice.apply(array, temp);