Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a couple of arrays that looks a bit like these:

arr['a'] = 'val1';
arr['b'] = 'val2';
arr['c'] = 'val3';

The index is not an integer, it is a string. I want to remove arr['b'] from the array completely. I have tried:

arr.splice('b', 1); 

It does not work, and it might be because the index in not an integer, according to w3schools this is the problem "index - Required. An integer".

A possible solution could be looping through all arrays and re-creating them with an integer index and then an array holding the custom indexes as values and the equivalent integer index as its index.

This seems like a tad unnecessary and a waste of resources, is there a smarter more effective and simpler solution?

Preferably an arr.splice that will work with a non-integer index.

I have looked through plenty of posts that covers how to remove elements from arrays by index and values, but none covers how to remove elements using a non-integer index.

Example posts that I have found: 0 1 2

Any and all help is greatly appreciated!

//Edit, used following as a solution.

function aObj() {
    this.a = ""; 
    this.b = [];
}

var aObjs = [];
aObjs.push(new aObj);
aObjs.push(new aObj);
aObjs.push(new aObj);

aObjs[0].a = "val1";

aObjs.splice(1, 1);

Looks a bit different than what I used in my first example, but this is more accurate towards how I used it. May not be the best way to do it, but it works.

share|improve this question
2  
JavaScript doesn't have associative arrays! Use an object. –  Vohuman Aug 31 '14 at 14:13
    
Is there a reason you're using an array? Sounds like you should use an object instead –  Nick White Aug 31 '14 at 14:13
    
Well... There might be no other reason than me not knowing I could... I shall look into this. Thanks. –  Daniel Aug 31 '14 at 14:17

3 Answers 3

up vote 2 down vote accepted

Don't use array for string indexes, use objects like bellow

var arr = {} //create a object

arr['a'] = 'val1'; //asign values
arr['b'] = 'val2';
arr['c'] = 'val3';

console.log(arr) //prints {a: "val1", b: "val2", c: "val3"}

delete arr['a']  //delete a key

console.log(arr) // prints {b: "val2", c: "val3"}
share|improve this answer
    
Used this as a start after reading undefined's and Nick White's answers, had to redo a lot of code, but was probably worth it. Updated question with solution. –  Daniel Aug 31 '14 at 17:11

Well it does not work, because you are using an array as a dictionary, which it's not. First of all use object for that. Second use delete to remove a property:

var dict = { 'a': 'val1', 'b': 'val2', 'c': 'val3' };
delete dict.a;
share|improve this answer

As said before, this is not an Array. If it should be an array, it looks like this

var arr = ['val1', 'val2', 'val3'];

Now you can use Array.splice to remove value 'val2':

arr.splice(1,1);
// or
arr.splice(arr.indexOf('val2'),1);
// or even
arr = arr.filter(function (v){ return v !== 'val2'});

If it should be an object, its declariation looks like:

var obj = {a: 'val1', b: 'val2', c: 'val3'};

And if you want to delete 'val2' whilst not knowing the key for it you can loop:

for (var key in obj) {
  if (obj[key] === 'val2';
  delete obj[key];
}
// or (mis)use Object.keys 
Object.keys(obj)
      .filter(function(v){
               return this[v] === 'val2' ? !(delete this[v]) : true;
              }, obj);

Knowing this, you can create a helper method for Objects and Arrays:

function removeByValue(objOrArr, value) {
   if (objOrArr instanceof Array && objOrArr.length) {
     var found = objOrArr.indexOf(value);
     if (found) { objOrArr.splice(found,1); }
   }
   if (objOrArr instanceof Object) {
     var keys = Object.keys(objOrArr);
     if (keys.length) {
       keys.filter(function(v){
               return this[v] === value ? !(delete this[v]) : true;
              }, objOrArr);
     }
   }
   return objOrArr;
}
// usage (using previous arr/obj)
removeByValue(arr, 'val2'); // arr now ['val1','val3']
removeByValue(obj, 'val2'); // obj now {a:'val1', c: 'val3'}

Example

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.