52

Array:

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

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

2
  • is it ok to copy it into new array without your index Commented Oct 4, 2012 at 9:41
  • The OP has confused the syntax of creating an object with creating an array. The correct syntax for an array would be: var arr = [ "abc", "def", "ghi" ]; Commented Jun 20, 2020 at 13:51

5 Answers 5

96

Use the splice method.

ArrayName.splice(indexValueOfArray,1);

This removes 1 item from the array starting at indexValueOfArray.

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

1 Comment

Useful to know that .splice also returns an array of the object(s) removed
30

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);

1 Comment

This one is more correct than the accepted answer.. +1 for pointing out that OP didn't use array "braces" ([]), and for object the syntax is invalid - meaning invalid in either case... just as an pointer, it would be nice to include delete in the answer to show how to delete from an object.
12

Your syntax is incorrect, you should either specify a hash:

hash = {abc: true, def: true, ghi: true};

Or an array:

arr = ['abc','def','ghi'];

You can effectively remove an item from a hash by simply setting it to null:

hash['def'] = null;
hash.def = null;

Or removing it entirely:

delete hash.def;

To remove an item from an array you have to iterate through each item and find the one you want (there may be duplicates). You could use array searching and splicing methods:

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

This finds the first index of "def" and then removes it from the array with splice. However I would recommend .filter() because it gives you more control:

arr.filter(function(item) { return item !== 'def'; });

This will create a new array with only elements that are not 'def'.

It is important to note that arr.filter() will return a new array, while arr.splice will modify the original array and return the removed elements. These can both be useful, depending on what you want to do with the items.

6 Comments

Setting an attribute to null doesn't remove it. You will need to use delete for that.
Already updated :) for the most part you don't need to use delete though.
You use delete when you want to remove an attribute. Setting it to null or undefined is bad practice since that would use up more memory then necessary.
Actually, using delete can be considered bad practice because it takes vastly longer than setting to null or undefined. However, both points are invalid, because the memory and time we're talking about it completely negligible in most cases.
typeof null != "undefined" so no point of suggesting it as a possible option, only delete is the correct way!
|
4
  1. Find the element in array and get its position
  2. Remove using the position

var array = new Array();
  
array.push('123');
array.push('456');
array.push('789');
                 
var _searchedIndex = $.inArray('456',array);
alert(_searchedIndex );
if(_searchedIndex >= 0){
  array.splice(_searchedIndex,1);
  alert(array );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

  • inArray() - helps you to find the position.
  • splice() - helps you to remove the element in that position.

Comments

2
delete arr[1]

Try this out, it should work if you have an array like var arr =["","",""]

3 Comments

This is bad practice. delete will not remove the item from the array, but set it to undefined.
seeing that it is the alphabet he might want to put something back in there
That's purely based on the assumption that those strings are the actual values and not placeholders, but a nice save nonetheless :)

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.