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.