How to use slice and splice in a proper way?

I’m trying to build a battleship game, the fleet is a 3d array contains the coordinates of ships
I’m trying to get the second cell of the second ship, but I keep getting empty arrays!

how to do this though I’m not sure if slice method here is the perfect way to do this

let arr=[[[1,2],[1,3]],[[5,6],[6,6]]]
let x=arr.slice(1);
let x2=x.slice(1);

Why are you using .slice? You only need to use array subscripting, like so:

let x = arr[1][1]

I need to delete the cell so later I can check if all cells of ship are hit

//all cells are removed -> the ship is sinked

So you’re wanting to get the cell [6,6] right?

let arr=[[[1,2],[1,3]],[[5,6],[6,6]]]
let x = arr[1][1].slice(0,2);
console.log(x) // gives [6,6]

And say you wanted cell [1,3] you would do

But slice just returns a copy to a new array object. You’re wanting to delete the cell right? To do that just use splice.

let arr=[[[1,2],[1,3]],[[5,6],[6,6]]]
console.log(arr); // returns [[[1,2],[1,3]],[[5,6]]]
1 Like

Thnx, I’ll try it asap