删除项目后自动重新编号对象键
Automatically renumber object key after item is removed
我有一个看起来像这样的对象:
0
Object { id="24105", x="10", y="4", more...}
1
Object { id="24104", x="6", y="5", more...}
2
Object { id="24103", x="10", y="6", more...}
3
Object { id="24102", x="10", y="3", more...}
4
Object { id="24101", x="8", y="6", more...}
5
Object { id="24100", x="6", y="1", more...}
6
Object { id="24099", x="10", y="8", more...}
7
Object { id="24098", x="8", y="3", more...}
8
Object { id="24097", x="8", y="7", more...}
9
Object { id="24096", x="10", y="2", more...}
10
Object { id="24095", x="8", y="1", more...}
11
Object { id="24094", x="6", y="2", more...}
12
Object { id="24093", x="6", y="8"}
13
Object { id="24092", x="8", y="8", more...}
14
Object { id="24091", x="6", y="4", more...}
15
Object { id="24090", x="6", y="7", more...}
16
Object { id="24089", x="10", y="1", more...}
17
Object { id="24088", x="4", y="8", more...}
18
Object { id="24087", x="8", y="2", more...}
19
Object { id="24086", x="6", y="6", more...}
20
Object { id="24085", x="10", y="7", more...}
21
Object { id="24084", x="6", y="3", more...}
22
Object { id="24083", x="8", y="5", more...}
23
Object { id="24082", x="10", y="5", more...}
24
Object { id="24081", x="4", y="7", more...}
如您所见,对象中的第 12 项包含的数据少于其他项,应将其删除。我已经使用 delete() 来做到这一点,但这并没有对其他项目重新编号,当我在 for 循环中循环遍历对象时会导致以后的错误。
我试过使用 splice(),但这会导致错误,因为(据我所知)我的对象是对象而不是数组。我不完全确定其中的区别。
有什么想法吗?
谢谢
你可以检测到没有。使用对象中的属性 Object.keys
并删除少于预期的属性。
Object.keys() returns an array whose elements are strings
corresponding to the enumerable properties found directly upon object.
The ordering of the properties is the same as that given by looping
over the properties of the object manually.
Object.keys(obj).length
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
我相信你有这样的东西,一个伪装成数组的对象:
{
0: { id:"24097", x:"8", y:"7",},
1: { id:"24096", x:"10", y:"2",},
2: { id:"24095", x:"8", y:"1",}
}
转换成真正的数组:
items = Object.keys( items ).map( function( index ){
return items[index];
});
并使用拼接。
我有一个看起来像这样的对象:
0
Object { id="24105", x="10", y="4", more...}
1
Object { id="24104", x="6", y="5", more...}
2
Object { id="24103", x="10", y="6", more...}
3
Object { id="24102", x="10", y="3", more...}
4
Object { id="24101", x="8", y="6", more...}
5
Object { id="24100", x="6", y="1", more...}
6
Object { id="24099", x="10", y="8", more...}
7
Object { id="24098", x="8", y="3", more...}
8
Object { id="24097", x="8", y="7", more...}
9
Object { id="24096", x="10", y="2", more...}
10
Object { id="24095", x="8", y="1", more...}
11
Object { id="24094", x="6", y="2", more...}
12
Object { id="24093", x="6", y="8"}
13
Object { id="24092", x="8", y="8", more...}
14
Object { id="24091", x="6", y="4", more...}
15
Object { id="24090", x="6", y="7", more...}
16
Object { id="24089", x="10", y="1", more...}
17
Object { id="24088", x="4", y="8", more...}
18
Object { id="24087", x="8", y="2", more...}
19
Object { id="24086", x="6", y="6", more...}
20
Object { id="24085", x="10", y="7", more...}
21
Object { id="24084", x="6", y="3", more...}
22
Object { id="24083", x="8", y="5", more...}
23
Object { id="24082", x="10", y="5", more...}
24
Object { id="24081", x="4", y="7", more...}
如您所见,对象中的第 12 项包含的数据少于其他项,应将其删除。我已经使用 delete() 来做到这一点,但这并没有对其他项目重新编号,当我在 for 循环中循环遍历对象时会导致以后的错误。
我试过使用 splice(),但这会导致错误,因为(据我所知)我的对象是对象而不是数组。我不完全确定其中的区别。
有什么想法吗?
谢谢
你可以检测到没有。使用对象中的属性 Object.keys
并删除少于预期的属性。
Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. The ordering of the properties is the same as that given by looping over the properties of the object manually.
Object.keys(obj).length
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
我相信你有这样的东西,一个伪装成数组的对象:
{
0: { id:"24097", x:"8", y:"7",},
1: { id:"24096", x:"10", y:"2",},
2: { id:"24095", x:"8", y:"1",}
}
转换成真正的数组:
items = Object.keys( items ).map( function( index ){
return items[index];
});
并使用拼接。