5

I have created a JavaScript object like

var obj={}
var prop = {}
prop.name= "name",
prop.value = "10"
obj[old_name] = prop;

I need to change the old_name to new_name. I have tried

obj[new_name] = obj[old_name];
delete obj[old_name];

And it works but, the object order gets changed.

For example:

{"obj1":{"name:name","value:10"},"obj2":{"name:name","value:10"}}

If I replace obj1 with objone, like this:

obj[objone ] = obj[obj1];
delete obj[obj1 ];

The object order changed to:

{"obj2":{"name:name","value:10"},"objone":{"name:name","value:10"}}]

But I need to change the property name alone and not the order, and I also try string replace but I think it is not the proper way, so please suggest me some ideas.

7
  • 5
    Objects have no order. Commented Oct 20, 2014 at 8:39
  • Why is the order a problem? Commented Oct 20, 2014 at 8:40
  • stackoverflow.com/questions/4647817/… Commented Oct 20, 2014 at 8:47
  • I've taken the liberty of fixing what I assume was a typo in your question (you used pre_name instead of old_name where I'm pretty sure you meant old_name). Also, it's "I" (capitalized), not "i" (lower case), in English, always. Commented Oct 20, 2014 at 8:47
  • I don't think this is a dupe of that question, @Cerbrus, because order is an important part of this one. Commented Oct 20, 2014 at 8:49

1 Answer 1

5

Objects have no order. Any apparent order you see is unspecified behavior and you cannot rely on it. They didn't when the question was asked, but they do now:

  1. Let keys be a new empty List.
  2. For each own property key P of O that is an integer index, in ascending numeric index order
    • Add P as the last element of keys.
  3. For each own property key P of O that is a String but is not an integer index, in property creation order
    • Add P as the last element of keys.
  4. For each own property key P of O that is a Symbol, in property creation order
    • Add P as the last element of keys.
  5. Return keys.

Your way of renaming the property is the only way of doing it: Creating a new one, then removing the old one. Doing that will change where it appears in the order of the object, because it was created after the previous property.

If you need order, use an array. While theoretically arrays don't have order either (because they're not really arrays), we have the convention that they have order, based on then numeric value of the indexes of the entries: The entry at index 0 is before the entry at index 1 and so on. (And modern JavaScript engines can and do use real arrays where possible.)

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

4 Comments

That's not the only way. You could use: JSON.parse(JSON.stringify(myObj).replace(old, new)). (Seriously though, don't use this)
@Cerbrus: :-) The only way that doesn't completely replace the object, perhaps.
@Cerbrus No, you couldn't. Raw textual replacement is not equivalent with the above code because it would replace each and every occurrence of old with new. Not only in one property name. Not only in keys.
@TheParamagneticCroissant: var old = '"myKey":', new = '"otherKey":'. Or use a regex replace. Sure, just dumping the keys themselves in there is asking for problems. Well, more problems than using this, in the first place.

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.