0

Here is what I need to do. I have an object that goes

{"MainASubB":"AB","MainBSubC":"BC"...}

Every once in a while I need to take out all attributes that start MainA prior to putting in a new attribute starting MainA. In the example above the object transformations would be

{"MainASubB":"AB","MainBSubC":"BC"...} =>
{"MainBSubC":"BC"...} => //MainASubB has now been taken out
{"MainASubD":"AB","MainBSubC":"BC"...};  A new MainA group attribute, MainASubD has now been added.

I am aware of what Javascript delete can do but on its own I dont think it quite takes me all the way there. I should mention that

  1. At times there may be no MainA group attribute present in the first place.
  2. Provided the code works as intended there can never be more than one MainA group attribute.

Stringifying the object, cleaning out the string as required, then de-stringifying it and then finally putting in the new MainA group attribute is certainly possible but I am wondering if there is another techique, perhaps one reliant on jQuery?, that will get me there faster.

4
  • You're looking for the delete operator. Commented Jun 4, 2013 at 13:13
  • What's wrong with delete? Where else does it need to take you? Commented Jun 4, 2013 at 13:14
  • Maybe you are looking for jQuery.extend [api.jquery.com/jQuery.extend/] Commented Jun 4, 2013 at 13:15
  • Perhaps my question was not clear. When I do need to take out the MainA group attribute all I will know is that there may be an attribute bearing the form MainA### without quite knowing what # might be Commented Jun 4, 2013 at 13:19

2 Answers 2

3

You have to iterate over the properties, compare each name and then delete the property:

for (var prop in obj) {
    if (prop.indexOf('MainA') === 0) { // property name starts with 'MainA'
        delete obj[prop];
        break; // since there can be only one
    }
}

I wouldn't use such "hierarchical" property names though. Why not use nested objects and just overwrite the value as you see fit?

For example:

var obj = {
    MainA: {
       SubA: '...'
    },
    MainB: {
       SubA: '...'
    }
};

and then it's just:

obj.MainA = {SubD: '...'};

or just add the "sub" value:

obj.MainA.SubD = '...';

This would be more flexible in the long run.

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

2 Comments

Thanks. Iteration is certainly a possiblity - in fact that is what I am currently doing. I posted the question in the hope that someone out here can suggest a neater solution. As for the hierarchical object attribute names - I have little control over that. It may not be a good idea in general but in the context of where this is being used it is not just the only solution but also the perfect one.
@DroidOS: There is no other reasonable solution than iterating over the property names. You could keep an extra object which holds a prefix -> property name mapping , e.g. var prefix_map = {MainA: 'MainASubA'};. Then you wouldn't have to iterate anymore, but you have to maintain the map.
1

Here is a generic function :

function removeProperties (obj, prop) {
  Object.keys (obj).forEach (
    function (p) {
      if (typeof prop === 'string' ? p.indexOf (prop) == 0 : prop.test (p)) 
        delete obj[p]; 
  });
  return obj;
}

The parameter obj is the object wheich you want to remove properties from. Parameter prop can be a string, 'MainA' for example, in which case any properties with names starting with that string will be removed. If 'prop is a regular expression then any properties whose names match it will be removed.

The removal is done in-place, i.e obj itself is modified, it is also returned as the result of the function.

See it in action here : http://jsfiddle.net/jstoolsmith/EpSxC/

2 Comments

Thanks. I am in fact sticking with simple iteration but I like your generic function which is why I am accepting your answer.
Thank you. I always strive to write generic functions, it makes reuse easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.