For example, I want to shift 1 element of arrays:
1D array:
const arr_1d=["a","b","c","d"];
arr_1d.push(arr_1d.shift())
document.write(JSON.stringify(arr_1d)); //["b","c","d","a"]
2D array:
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"]]
How to I do it with 3D array
// (eg:
[[["a","b"],[["c","d"]],[["e","f"],[["g","h"]]]
// to
[[["b","c"],[["d","e"]],[["f","g"],[["h","a"]]]
//)
I tried:
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"]]]
the output is
[[["b","c"],["d","a"]],[["f","g"],["h","e"]]]
instead of
[[["b","c"],[["d","e"]],[["f","g"],[["h","a"]]]
, which is not working.
CodePudding user response:
The following should do it:
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));
I simply flatten the whole array into a new one dimensional array one
, "rotate" the values there once and then feed the elements back into the already existing array arr_3d
, replacing the original values there.
CodePudding user response:
This code give you the expected answer.In here I flatten the whole 3d arr and re assign that element to the correct position according to the shift order.
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));
CodePudding user response:
this one work in any dimensions...
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;}
CodePudding user response:
Adding to the @Carsten-Massmann answer, we can also create a recursive function to achieve n level of shifting of array items.
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))));