简体   繁体   中英

Remove element from Javascript associative array using array value

I am trying to remove an element from a Javascript associtive array using the value to find it, but I am having trouble. I have tried splice and JQuery's grep method and neither have worked for me. This is what I currently have.

var array_path = new Array();

function bulk_upload(){

var temp_array = new Object();

for (var i = 1; i<8; i++){

        temp_array[i] = $('#path' + i).val();

        if(temp_array[i]!='' && temp_array[i]!=null){

            array_path['path' + i] = $('#path' + i).val();

        }

}
process_txt();
}

function process_txt(){

//alert(array_path.indexOf(full_path)); //returns nothing

var removed_element = array_path.splice(getKey(array_path), 1);

//array_path = $.grep(array_path, function(val) { return val != full_path; });

alert(removed_element);//return nothing, just blank alert box

}

function getKey(data) {
  for (var prop in data)
    return prop;
}

The way to do this is to use the delete operator.

delete array_path[getKey(array_path)]

Some Background Information

In JavaScript, almost everything descends from Object.prototype . JavaScript, being an open and dynamic language allows you to create/modify properties of objects by simple assignment. This is very similar to what an associative array -- a structure that contains keyed values.

Under the hood an array is just an object that descends from Array.prototype with numeric keys and a special property called length . The length property just returns one greater than the highest numeric property. In essence, an Array is an object with different semantics.

If you're wanting an associative array then Array is not the object you want to descend from. You would want to descend directly from Object . There are two ways to do that, you could either use the new operator or an empty object literal. The syntax for both is below:

var o = new Object();
var o = {};

The second is preferred since it's a little bit more concise.

I wrote a blog post about this a while back, have a look if you want a little bit more info.

There is no such thing in JavaScript as an "associative array" per se. The data structure which corresponds to this concept is simply a JavaScript Object.

Of course, a JavaScript Array (like essentially everything in JavaScript) is an Object, but one with additional capabilities. So you can use an Array as a key-value map, but it's really not the correct structure for that.

To remove a key from an Object, you just do something like this:

var myObj = {};
var myKey = "blah";

myObj[myKey] = 1234; // Adds or updates value for "blah" to 1234.

delete myObj[myKey]; // Removes key-value pair for "blah".

Have you tried delete hash.someKey; ?

You can give your object a remove method, or use apply or call to use another object's remove method, if defined.

function myObj(members){
 for(var p in members) this[p]= members[p];
}

myObj.prototype.remove= function(val){
 for(var p in this){
  if(this[p]=== val) delete this[p];
 }
 return this;
}

myObj.prototype.toString= function(){
 var  A= [];;
 for(var p in this){
  if(this.hasOwnProperty(p)){
   A.push(p+':'+this[p])
  }
 }
 return '{'+A.join(', ')+'}';
}

var O= new myObj({a: 1, b: 10, c: 100});
alert(O)
O.remove(10);
alert(O)

I'm not psychic, so I can only guess that you wanted to accomplish something like this:

var paths = [];

function getPaths() {
    for(var i = 1; i < 8; ++i) {
        var value = $('#path' + i).val();
        if(value) paths.push(value);
    }
}

function process() {
    var firstPath = paths.shift();
    // do stuff
}

getPaths();
if(paths.length) process();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM