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

发布于 2024-09-15 09:56:51 字数 176 浏览 2 评论 0原文

我有一个对象,其中包含名为 [0...10] 的变量和几个具有正常名称的变量。我用delete obj[3]删除其中之一。然后这个序列中有一个间隙。我现在想从 [0...10] 开始排列它们。我的第一个想法是循环并重命名键(变量)。我怎样才能做到呢?

PS - 对象无法更改其结构或转换为数组以使用 splice()..

I am having an object with variables named [0...10] and few variables with normal names. I delete one of them with delete obj[3]. Then there is a gap in this sequence. I want to arrange them now from [0...10]. My first though is to loop through and rename keys(variables). How I can do it?

P.S. - Object cannot change its structure or be converted to an array to use splice()..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞清仓 2024-09-22 09:56:51

那么你为什么不尝试一下你的第一个想法呢?

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文