1

I have a javascript callback function which queries my database using a set of parameters. The parameters are set using an object. How can I set the name of one of the properties dynamically?

Right now I have them all just written out. It works fine, it's just a little long. I just need to change the value of two property names. Where propertyNameToChange is in my example code.

var paramToChange = { /* I need to change the name of this property */
    propertyNameToChange: { /* And the name of this property */
      url: { 
        ComparisonOperator: 'EQ',
        AttributeValueList: [{
          S: alreadySetWithVar /* This is already set using a var */
        }]
      }
    },
    TableName: 'ximoRep',
    IndexName: 'url-index'
  }
.... // I have about 10 of these written out. Then my callback function queries using an array using the var name for the object.

var params = [paramOne, paramTwo, paramThree, paramFour, paramFive, paramSix, paramSeven, paramEight]; 

2 Answers 2

2
paramToChange['newPropName'] = paramToChange['propertyNameToChange'];
delete paramToChange['propertyNameToChange'];

Fixed.

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

Comments

2

Javascript does not have a means of changing the name of a property. What you can do is to assign one property to another new property name and then delete the original property.

If you have this data structure:

var data = {
    prop1: {greeting: "hello"}
};

And, you want to change prop1 to otherProp, you can do it like this:

data.otherProp = data.prop1;
delete data.prop1;

This assigns the value of the original property to a new property and then removes the original property.

The end result is:

var data = {
    otherProp: {greeting: "hello"}
};

A general purpose function for doing this that would even work with propeties that have special characteristics such as getters, setters or other custom attributes would use a propertyDescriptor like this:

function renameProp(obj, oldPropName, newPropName) {
    var descriptor = Object.getOwnPropertyDescriptor(obj, oldPropName);
    Object.defineProperty(obj, newPropName, descriptor);
    delete obj.oldPropName;
}

This assumes the oldPropName property is configurable and the object isn't frozen.

5 Comments

It's probably best to use propertyDescriptors when possible
@PitaJ - that extra complication is useful if you think the properties have special attributes. If they're just regular properties, then there's no need.
I was just talking in the general sense. A more general solution would require the use of propertyDescriptors to really cover all of the angles.
@PitaJ - yeah, I agree. Just not needed for just a bunch of data.
@PitaJ - I added a function that uses a property descriptor.

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.