1

My Object

How to remove keys like UserID, UserName from it... i mean selected keys... delete operator is not working in my case.

for (i=0 i <obj.length; i ++) {
    delete obj[i]['keyName']; 
}

The above does not work, neither throws an error. Any other way...

2
  • 1
    delete should work. Show the full code Commented Jul 21, 2011 at 12:32
  • You're probably using for loop the wrong way. Commented Jul 21, 2011 at 12:34

5 Answers 5

6

You missed the ; after i=0.

Additionally, obj needs to be MyObject.PatientVitalsGetResult.Vitals

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

1 Comment

MyObject.PatientVitalsGetResult.Vitals actualy
1

Don't use delete; it will set the element to undefined instead of removing it. Instead, use splice.

var i;
for(i = 0; i < obj.length; i++){
    obj[i].splice('keyName',1);
}

1 Comment

I'm pretty sure that the goal is to remove a named key from each object in an array, not individual array elements.
1

There is no standard way for this, afaik. You will need to perform a conditional copy of the old Object filtering the unwanted properties:

var oldObject = { /* your object */ } ;
var newObject = { } ;

var filter = { "UserID": true , "UserName": true } ;

for(var key in oldObject) 
    if( !(key in filter) ) newObject[key] = oldObject[key] ; 

Then use the acquired newObject in the following code.

1 Comment

I don't understand your criticism. How is this approach "costly"? How is it "not needed" (especially if there is no other way to remove entries from an Object).
1
var vitals = obj["PatientVitalsGetResult"]["Vitals"];


for (i=0; i < vitals.length; i++) {
    delete(vitals[i]["UserID"])
};

Comments

0

How about this solution....

Array.prototype.containsValue = function (value) {
        for (var k in this) {
            if (!this.hasOwnProperty(k)) {
                continue;
            } //skip inherited properties
            if (this[k] == value) {
                return true;
            }
        }
        return false;
    };

for (var key in Object) {
    var unwantedKeys = ['UserName', 'UserID'];
    if (unwantedKeys.containsValue(key)) continue;
    // Play with your Object it won't contain these unwanted keys.
}

Delete should work, i am not sure about the mistake you are doing..

http://jsfiddle.net/spechackers/43hTD/

3 Comments

@John: as a reminder, it's generally bad practice to use a for...in loop on arrays. Order of iteration is not guaranteed and any non-index enumerable properties will also be iterated over. Just something to think about if you're implementing this solution.
@Andy E: Are you talking about the fiddle or my other response... i was just showing the OP that delete works...
@Vinothbabu: I'm talking about the Array.prototype.containsValue function here, which uses a for...in loop to iterate over this. this would be an array, so for...in isn't really appropriate to use.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.