I would like to create a copy of an array but switching the position of the most inner arrays. This is the original array:
const primera = [
[["a","b"],["c","d"]],
[["a","c"],["b","d"]],
[["a","d"],["c","b"]]
]
I want to create another an array like this but without modifying the "primera" array:
[
[["b","a"],["d","c"]],
[["c","a"],["d","b"]],
[["d","a"],["b","c"]]
]
I have tried this but when I console.log both arrays, the original array is modified.
const primera = [
[["a","b"],["c","d"]],
[["a","c"],["b","d"]],
[["a","d"],["c","b"]]
]
const doubleRound = (arr) => {
const newArr = [...arr]
for(let i=0; i<newArr.length;i++) {
for(let j=0; j<newArr[i].length;j++) {
newArr[i][j] = [newArr[i][j][1],newArr[i][j][0]]
}
}
return newArr
}
const segunda = doubleRound(primera)
console.log(primera)
console.log(segunda)
Any idea how can I do it? Thank you.