简体   繁体   English

在javascript中重命名对象键(变量)

[英]Renaming object keys(variables) in javascript

I am having an object with variables named [0...10] and few variables with normal names. 我有一个对象,该对象的变量名为[0 ... 10],很少有普通名称的变量。 I delete one of them with delete obj[3]. 我使用delete obj [3]删除其中之一。 Then there is a gap in this sequence. 然后在这个序列中有一个缺口。 I want to arrange them now from [0...10]. 我现在想从[0 ... 10]开始安排它们。 My first though is to loop through and rename keys(variables). 我的第一个方法是循环遍历并重命名键(变量)。 How I can do it? 我怎么能这样做?

PS - Object cannot change its structure or be converted to an array to use splice().. PS-对象无法更改其结构或将其转换为数组以使用splice()。

So why haven't you tried your first thought? 那么,为什么不尝试您的第一个想法?

delete obj[k]
for ( var i = k; i < N - 1; ++i) {
   obj[i]=obj[i+1];
}
delete obj[N];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM