I have created a JavaScript object like
我创建了一个JavaScript对象
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
我需要将old_name更改为new_name。我努力了
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:
如果我用objone替换obj1,像这样:
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.
但是我需要单独更改属性名称而不是顺序,我也尝试使用字符串替换,但我认为这不是正确的方法,所以请给我一些想法。
1 个解决方案
#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:
对象没有订单。您看到的任何明显的订单都是未指明的行为,您不能依赖它。问题被提出时他们没有,但他们现在这样做:
- Let keys be a new empty List.
让密钥成为新的空列表。
- 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.
添加P作为键的最后一个元素。
对于作为整数索引的O的每个自有属性键P,以升序数字索引顺序将P添加为键的最后一个元素。
- 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.
添加P作为键的最后一个元素。
对于每个属性的键P,它是一个String但不是整数索引,在属性创建顺序中将P添加为键的最后一个元素。
- For each own property key P of O that is a Symbol, in property creation order
- Add P as the last element of keys.
添加P作为键的最后一个元素。
对于作为符号的O的每个属性键P,在属性创建顺序中将P添加为键的最后一个元素。
- 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.)
如果您需要订购,请使用数组。虽然理论上数组也没有顺序(因为它们不是真正的数组),但是根据条目索引的数值,我们有它们有顺序的约定:索引0处的条目在条目之前索引1等。 (现代JavaScript引擎可以并且确实在可能的情况下使用真实数组。)
#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:
对象没有订单。您看到的任何明显的订单都是未指明的行为,您不能依赖它。问题被提出时他们没有,但他们现在这样做:
- Let keys be a new empty List.
让密钥成为新的空列表。
- 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.
添加P作为键的最后一个元素。
对于作为整数索引的O的每个自有属性键P,以升序数字索引顺序将P添加为键的最后一个元素。
- 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.
添加P作为键的最后一个元素。
对于每个属性的键P,它是一个String但不是整数索引,在属性创建顺序中将P添加为键的最后一个元素。
- For each own property key P of O that is a Symbol, in property creation order
- Add P as the last element of keys.
添加P作为键的最后一个元素。
对于作为符号的O的每个属性键P,在属性创建顺序中将P添加为键的最后一个元素。
- 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.)
如果您需要订购,请使用数组。虽然理论上数组也没有顺序(因为它们不是真正的数组),但是根据条目索引的数值,我们有它们有顺序的约定:索引0处的条目在条目之前索引1等。 (现代JavaScript引擎可以并且确实在可能的情况下使用真实数组。)