Clem
Clem

Reputation: 11824

How to remove a value from array? javascript

I create an object thus:

var savingArray = new Array({doctorId: "something", username: "something", password: "something", givenName: "something", familyName: "something", address: "something", zip:"something", emailAddress: "something", phoneNumber: "something", labs: "something", defaultLab: "something"});

Now i wish to remove every value of objects...I would like to end up with savingArray such that:

savingArray == ({doctorId: "", username: "", password: "", givenName: "", familyName: "", address: "", zip:"", emailAddress: "", phoneNumber: "", labs: {}, defaultLab: ""});

So all i want is to change all values to "" dinamicaly..

savingArray has never the same object so a can't do like this:

savingArray[0].doctorId = "";

Upvotes: 2

Views: 234

Answers (6)

Rune FS
Rune FS

Reputation: 21752

Properties of a JavaScript object can be treated as a key value pair and it's possible to iterate over the object so what you need to do is first iterate over the objects in the array

for(var i = 0;i < savingArray.length; i++)  { ... }

and then iterate over the properties of each object. However you also want to test the relation between the property and the object. Putting it all together you end up with code like this:

var i,prop,current;
//iterate over the objects in the array
for(i = 0;i<savingArray.length);i++){
  current = savingArray[i];
  //iterate over all the properties of the object
  for(prop in current){
     //test the relationship of the property and the object
     if(current.hasOwnProperty(prop)){
        //assign the empty string to the property
        current[prop] = "";
     } 
  }
}

or in a jQuery version

$.each(savingArray, function(i, obj) {
    $.each(obj, function(key) {
        if(obj.hasOwnProperty(key)){
           obj[key] = '';
        }
    });
});

I expect the previous to be more performant than the jQuery but find the jQuery a little easier to read due to being more condensed

Upvotes: 1

Florian Margaine
Florian Margaine

Reputation: 60835

savingArray.forEach( function( el ) {
    Object.keys( el ).forEach( function( key ) {
        el[ key ] = '';
    });
});

Self-explaining code using forEach and Object.keys. This won't work in IE6/7/8 of course.

Upvotes: 3

thecodeparadox
thecodeparadox

Reputation: 87073

$.each(savingArray, function(index, obj) {
  $.each(obj, function(k, val) {
     obj[k] = "";
  });
});

DEMO

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318718

for(var i = 0; i < savingArray.length; i++) {
    var obj = savingArray[i];
    for(var key in obj) {
        obj[key] = '';
    }
}

Or since you are using jQuery you an also use its $.each() function to iterate:

$.each(savingArray, function(i, obj) {
    $.each(obj, function(key, value) {
        obj[key] = '';
    });
});

In case something modified Object.prototype (e.g. added methods to it - something that usually shouldn't be done) you'd need to add an if(obj.hasOwnProperty(key)) check and only set the value if that check succeeds.

Upvotes: 6

Guffa
Guffa

Reputation: 700780

As you are using jQuery, use it for looping arrays and objects also:

$.each(savingArray, function(i, item){
  $.each(item, function(key){
    item[key] = "";
  });
});

Upvotes: 1

VisioN
VisioN

Reputation: 145458

Is that you are looking for?

for (var i = 0; i < savingArray.length; i++) {
    for (var key in savingArray[i]) {
        if (savingArray[i].hasOwnProperty(key))
            savingArray[i][key] = "";
    }
}

DEMO: http://jsfiddle.net/NfBqn/

Upvotes: 1

Related Questions