使用Javascript:使用递归到位拼合多维数组 [英] Javascript: Flatten multidimensional array in place using recursion
问题描述
我有以下的code以展多维数组
变种X = [[[2,3],4],5,[6,7]];扁平化的功能(ARR){ 对于(VAR I = 0; I< arr.length;我++){
如果(ARR [I] .constructor ===阵列){
subArr = ARR [I]
//根据所需子阵列的空间向下移动阵列
为(VAR J = arr.length + subArr.length - 2; J> I + subArr.length - 1; j--){
ARR [J] = ARR [J - subArr.length + 1];
}
属于他们的地方//插入子数组元素
对于(VAR K = 0; K< subArr.length; k ++){
改编[I + K] = subArr [K];
}
//看改编[I]再次情况下,在子数组的第一个元素是另一个数组;
一世 - ;
}
}
}扁平化(X);
JSBin在这里: http://jsbin.com/gazagemabe/edit?js,console一>
我想这样做,使用递归,但我最终被卡住。我试图做到这一点不保持一个临时数组,但后来事情似乎落在了混乱。我觉得我缺少的递归一些核心原则。
我意识到我需要一个基本情况。我能想出的唯一情况是,当目前在扁平化数组有没有子阵。但是,因为我总是传递数组引用,我什么都没有回来。
我psuedo- code是
功能扁平化(ARR) 通过ARR循环
如果ARR [指数]是一个数组
增加ARR [指数]长数组的长度
扁平化(ARR [指数])
其他
//不清楚该怎么在这里做修改原来的数组
由内而外做递归扁平化。首先调用扁平化
函数,你发现在阵列上,然后移动(现平)阵列的内容到父阵列。环向后遍历数组,这样你就不必调整循环变量为插入的项目。
当你知道你的插入是平的数组,你可以使用拼接
方法带项目,以取代一个数组。
它的工作原理是这样的:
开始
[[[2,3],4],5,[6,7]扁平化[6,7](这已经是平的),并插入:
[[[2,3],4],5,6,7]弄平[[2,3] 4]递归地调用弄平[2,3],并且阵列中的刀片:
[[2,3,4],5,6,7]
然后将其插入[2,3,4]:
[2,3,4,5,6,7]
code:
功能扁平化(ARR){\r
对于(VAR I = arr.length - 1; I> = 0;我 - ){\r
如果(ARR [I] .constructor ===阵列){\r
扁平化(ARR [I]);\r
Array.prototype.splice.apply(ARR,[1,1] .concat(ARR [I]));\r
}\r
}\r
}\r
\r
变种X = [[[2,3],4],5,[6,7]];\r
\r
扁平化(X);\r
\r
//显示结果摘录\r
的document.write(JSON.stringify(X));
\r
I have the following code that flattens a multidimensional array
var x = [[[2, 3], 4], 5, [6, 7]];
function flatten(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i].constructor === Array) {
subArr = arr[i];
// Shift the array down based on the space needed for the sub array
for (var j = arr.length + subArr.length - 2; j > i + subArr.length - 1; j--) {
arr[j] = arr[j - subArr.length + 1];
}
// Insert sub array elements where they belong
for (var k = 0; k < subArr.length; k++) {
arr[i + k] = subArr[k];
}
// Look at arr[i] again in case the first element in the subarray was another array;
i--;
}
}
}
flatten(x);
JSBin here: http://jsbin.com/gazagemabe/edit?js,console
I want to do this using recursion but I end up getting stuck. I'm trying to do it without keeping a temp array, but then things seem to fall out of whack. I feel like I'm missing some core principal of recursion.
I realize I need a base case. The only case I can come up with is when the array currently in flatten has no subarrays. But since I'm always passing the array by reference, I have nothing to return.
My psuedo-code is
function flatten(arr)
loop through arr
if arr[index] is an array
increase the array length arr[index] length
flatten(arr[index])
else
// unsure what to do here to modify the original array
Do the recursive flattening from the inside out. First call the flatten
function on the array that you find, then move the contents of that (now flat) array into the parent array. Loop backwards through the array so that you don't have to adjust the loop variable for the items that are inserted.
As you know that the arrays that you insert are flat, you can use the splice
method to replace an array with its items.
It works like this:
start with
[[[2, 3], 4], 5, [6, 7]]
flatten [6,7] (which is already flat) and insert:
[[[2, 3], 4], 5, 6, 7]
flatten [[2, 3], 4] recursively calls flatten [2,3] and inserts in that array:
[[2, 3, 4], 5, 6, 7]
then it inserts [2, 3, 4]:
[2, 3, 4, 5, 6, 7]
Code:
function flatten(arr) {
for (var i = arr.length - 1; i >= 0; i--) {
if (arr[i].constructor === Array) {
flatten(arr[i]);
Array.prototype.splice.apply(arr, [i, 1].concat(arr[i]));
}
}
}
var x = [[[2, 3], 4], 5, [6, 7]];
flatten(x);
// Show result in snippet
document.write(JSON.stringify(x));
这篇关于使用Javascript:使用递归到位拼合多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!