Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Array Value By index in jquery

Tags:

jquery

Array:

var arr = {'abc','def','ghi'}; 

I want to remove above array value 'def' by using index.

like image 512
Baskar Avatar asked Oct 04 '12 09:10

Baskar


People also ask

How to remove an element from array by index in jQuery?

Use the splice method. ArrayName. splice(indexValueOfArray,1); This removes 1 item from the array starting at indexValueOfArray .

How to remove particular value from array in jQuery?

jQuery: Remove a specific value from an array using jQuery Remove a specific value from an array using jQuery. JavaScript Code: var y = ['Red', 'Green', 'White', 'black', 'Yellow']; var remove_Item = 'White'; console. log('Array before removing the element = '+y); y = $.

How to remove from value in jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.


2 Answers

Use the splice method.

ArrayName.splice(indexValueOfArray,1); 

This removes 1 item from the array starting at indexValueOfArray.

like image 85
Kumaran Avatar answered Sep 30 '22 12:09

Kumaran


Your example code is wrong and will throw a SyntaxError. You seem to have confused the syntax of creating an object Object with creating an Array.

The correct syntax would be: var arr = [ "abc", "def", "ghi" ];

To remove an item from the array, based on its value, use the splice method:

arr.splice(arr.indexOf("def"), 1); 

To remove it by index, just refer directly to it:

arr.splice(1, 1); 
like image 45
mekwall Avatar answered Sep 30 '22 12:09

mekwall