Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all other values in an array except the ith element?

I have a function using an array value represented as

 markers[i] 

How can I select all other values in an array except this one?

The purpose of this is to reset all other Google Maps images to their original state but highlight a new one by changing the image.

like image 801
blarg Avatar asked Mar 12 '13 12:03

blarg


People also ask

How do you exclude an element from an array?

pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.

How do you select a value in an array?

You select a value from an array by referring to the index of its element. Array elements (the things inside your array), are numbered/indexed from 0 to length-1 of your array.


1 Answers

Use Array​.prototype​.splice to get an array of elements excluding this one.

This affects the array permanently, so if you don't want that, create a copy first.

var origArray = [0,1,2,3,4,5]; var cloneArray = origArray.slice(); var i = 3;  cloneArray.splice(i,1);  console.log(cloneArray.join("---")); 
like image 51
DhruvPathak Avatar answered Sep 21 '22 02:09

DhruvPathak