0

I have a java script array. I want to remove specific item from array using value. I can not remove by key because i am making dynamic functionality and there i have only value so i have to remove specific item from using value.

var arr =   Array();
arr['a']    =   123;
arr['b']    =   234;
arr['c']    =   345;
arr['d']    =   456;
arr['e']    =   567;
user3454835
  • 103
  • 1
  • 11
  • @FarhadJabiyev The answers there use `splice()`, which only works for numeric arrays. This question is actually about object properties. – Barmar Apr 17 '14 at 05:04
  • You really confused lots of answerers by calling this an array. It's an object. In JS, arrays are only indexed using integers. When you use strings, they're object properties. – Barmar Apr 17 '14 at 05:11
  • 1
    possible duplicate of [Remove property by value from object](http://stackoverflow.com/questions/17134386/remove-property-by-value-from-object) – Barmar Apr 17 '14 at 05:12

5 Answers5

2

If you have repeated values you wish to delete:

var arr =   Array();
arr['a']    =   123;
arr['b']    =   234;
arr['c']    =   345;
arr['d']    =   456;
arr['e']    =   567;

function removeEl(arr,val){
    for (var i in arr){
        if(arr[i]==val){
            delete arr[i];
        }
    }
}
removeEl(arr,234)
console.log(arr)
juvian
  • 15,212
  • 2
  • 30
  • 35
2

You seem to be treating JavaScript arrays as associative arrays, however, this isn't how arrays in JavaScript work. With the format your data is taking, I would suggest having them stored as an object:

var obj = {
   "a": 123,
   "b": 234,
   "c": 345,
   "d": 456,
   "e": 567
};

You can then iterate and remove using this function:

function removeByValue(object, value) {
   for (var e in object) {
      if (object[e] === value) {
         delete object[e];
      }
   }
}
Anthony Calandra
  • 1,230
  • 2
  • 10
  • 16
0

First, you are creating an array, then treating it like an object.

Your JavaScript life will be MUCH easier if you pretend that arrays can only take integer properties.[1]

So, what you really mean to do is this:

var obj =   {};
obj['a']    =   123;
obj['b']    =   234;
obj['c']    =   345;
obj['d']    =   456;
obj['e']    =   567;

Or, in object literal format:

var obj =   {
  a:   123,
  b:   234,
  c:   345,
  d:   456,
  e:   567
};

To delete a property from an object, simply use the delete keyword:

delete obj.b;

[1] Technically, arrays are also objects -- but arrays are special-purpose objects.

Jeremy J Starcher
  • 21,760
  • 5
  • 48
  • 70
0

.indexOf won't work because it isn't an array so you should find and remove this value manually

var arr =   Array();
arr['a']    =   123;
arr['b']    =   234;
arr['c']    =   345;
arr['d']    =   456;
arr['e']    =   567;

var result={};
var value_to_remove = 123;
for(key in arr){
    if(arr[key]!=value_to_remove)
        result[key] = arr[key];
}
Aray Karjauv
  • 1,998
  • 21
  • 37
-1

In PHP use array_search to find key then use unset() destroys the specified variables.

edited

in Jquery :

to find index use inArray jQuery.inArray( "123", arr )

and to unset use delete

delete arr.index; 
Saurabh Chandra Patel
  • 9,983
  • 3
  • 77
  • 72