如何移动三维数组的1个元素



例如,我想移动数组的1个元素:

1D阵列:

const arr_1d=["a","b","c","d"];
arr_1d.push(arr_1d.shift())
document.write(JSON.stringify(arr_1d)); //["b","c","d","a"]

2D阵列:

const arr_2d=[["a","b"],["c","d"]];
for(let i=0;i<arr_2d.length;i++){
arr_2d[i==0?arr_2d.length-1:i-1].push(arr_2d[i].shift());
}
document.write(JSON.stringify(arr_2d)); //[["b","c"],["d","a"]]

如何使用3D阵列

// (eg:
[[["a","b"],[["c","d"]],[["e","f"],[["g","h"]]] 
// to 
[[["b","c"],[["d","e"]],[["f","g"],[["h","a"]]]  
//)

我试过了:

const arr_3d=[[["a","b"],["c","d"]],[["e","f"],["g","h"]]];
for(let i=0;i<arr_3d.length;i++){
for(let j=0;j<arr_3d[i].length;j++){
arr_3d[i][j==0?arr_3d.length-1:j-1].push(arr_3d[i][j].shift());
}
}
document.write(JSON.stringify(arr_3d)); //[[["b","c"],["d","a"]],[["f","g"],["h","e"]]]

输出是

[[["b","c"],["d","a"]],[["f","g"],["h","e"]]]

而不是

[[["b","c"],[["d","e"]],[["f","g"],[["h","a"]]]

,不起作用。

以下操作应该完成:

const arr_3d=
[
[
["a","b"],
["c","d"]
],
[
["e","f"],
["g","h"]
]
];
const one=arr_3d.flat(2); // flatten
one.push(one.shift()); // rotate values
const three=arr_3d.forEach(a3=>
a3.forEach(a2=>
a2.forEach((_,i,a)=>
a[i]=one.shift()))); // feed back
document.write(JSON.stringify(arr_3d));

我简单地将整个阵列展平为新的一维阵列one;"旋转";在那里的值一次,然后将元素反馈到已经存在的数组arr_3d中,替换那里的原始值。

添加到@Carsten Massmann答案中,我们还可以创建一个递归函数来实现数组项的n级移位。

const arr_1d = ['a', 'b', 'c'],
arr_2d = [["a","b"],["c","d"],["e","f"],["g","h"]],
arr_3d = [[['a','b'],['c','d']],[['e','f'],['g','h']]];

const flattenArray = a => (a=a.flat(Infinity), a.push(a.shift()), a);
const mappedArr = (a,f) => a.map(o=> Array.isArray(o) ? mappedArr(o,f) : f.shift())
console.log(JSON.stringify(mappedArr(arr_1d, flattenArray(arr_1d))));
console.log(JSON.stringify(mappedArr(arr_2d, flattenArray(arr_2d))));
console.log(JSON.stringify(mappedArr(arr_3d, flattenArray(arr_3d))));

这段代码为您提供了预期的答案。在这里,我将整个三维arr展平,并根据移位顺序将该元素重新分配到正确的位置。

const arr_3d=[[["a","b"],["c","d"]],[["e","f"],["g","h"]]];
const tmp=[]
for(let i=0;i<arr_3d.length;i++){
for(let j=0;j<arr_3d[i].length;j++){
for(let k=0; k<arr_3d[i][j].length;k++){
tmp.push(arr_3d[i][j][k])
}

}
}
n=1
for(let i=0;i<arr_3d.length;i++){
for(let j=0;j<arr_3d[i].length;j++){
for(let k=0; k<arr_3d[i][j].length;k++){
arr_3d[i][j][k]=tmp[n%tmp.length]
n+=1
}

}
}
document.write(JSON.stringify(arr_3d));

这个可以在任何维度上工作。。。

const
arr_1d = ['a','b','c','d']
, arr_2d = [['a','b'],['c','d']]
, arr_3d = [[['a','b'],['c','d']],[['e','f'],['g','h']]]
;
console.log( JSON.stringify( universalShift(arr_1d) ) )
console.log( JSON.stringify( universalShift(arr_2d) ) )
console.log( JSON.stringify( universalShift(arr_3d) ) )

function universalShift( arrXdim )
{
let shift=null, lastPath=null, indx=0;
parse(arrXdim)
if (lastPath) setLast( lastPath, shift );
return arrXdim;
function parse(arr, sPath='[]', level=0)
{
let path = JSON.parse(sPath);
for(let i=0;i<arr.length;i++)
{
path[level] = i;
if (Array.isArray(arr[i]))  
parse(arr[i], JSON.stringify(path), level +1 );
else 
{
if (shift===null)  shift = arr[i];
else               setLast( lastPath, arr[i] );
lastPath = path.join('-')
} } }
function setLast( last, val )
{
last.split('-')
.reduce( (a,k,l,{[l+1]:n})=> 
{
if (n) return a[k];
a[k] = val;
}, arrXdim )
}
}
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}

相关内容

  • 没有找到相关文章

最新更新