3

I am trying to remove an array element from an array of arrays. but i am not able to do it.

JS code:

var blueTiles = [];
blueTiles.push([1, 1]);
blueTiles.push([2, 2]);
blueTiles.push([3, 3]);

var removeCoord = [2, 2];

var index = blueTiles.indexOf(removeCoord);
if (index > -1) blueTiles.splice(index, 1);

but here i am trying to get position of array element in blueTile array ans remove using splice function. but i am getting index value as -1 even though [2, 2] exists in it

please help me in solving this.

1 Answer 1

1

Use this as your index (checks all array elements against the remoceCoord elements)

var index = blueTiles.findIndex(x=>x.every((y,i)=>y===removeCoord[i]))

var blueTiles = [];
blueTiles.push([1, 1]);
blueTiles.push([2, 2]);
blueTiles.push([3, 3]);

var removeCoord = [2, 2];

var index = blueTiles.findIndex(x => x.every((y, i) => y === removeCoord[i]))
if (index > -1) blueTiles.splice(index, 1);

console.log(blueTiles)

Sign up to request clarification or add additional context in comments.

3 Comments

thank u for the reply, i am getting some syntax error in this, can you please recheck.
if you rut the code snippet, you can see that there are no syntax errors,, which error do you get what does it say
ow sorry, its my mistake i made a typo, thank you Eugen sunic, can you explain me how does it work and why my code dosent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.