从js对象中删除带编号的键



我有一个js对象,其id格式类似{'1': ['property1', 'property2'...], '2': ['property1', 'property2'...]...}

我使用的是对象而不是列表,以备以后命名属性时使用。

我使用了js代码

for(i = 0; i < Object.keys(currentFile).length; i++) {
if(i == activeBoxId) {
delete currentFile[i];
};
if(i > activeBoxId) {
currentFile[i - 1] = currentFile[i];
delete currentFile[i];
};

尝试在删除某个特定索引时保持数字顺序,以避免找不到id号时出错。然而,它似乎不起作用,因为当我打印currentFile时,它会像1、2、3、5、6一样——删除我想要的属性,但似乎不会删除下一个。你能帮忙吗?

完整上下文,使用删除框

在我看来,你是在从内而外构建它。您不应该将不同数据的数组放在带编号的对象中,而应该将对象放在数组中。如果要命名属性集,只需将名称与属性一起添加即可。这也将为您提供添加和删除属性的灵活性,而不会破坏所有内容的顺序。

例如,我看到这就是你所拥有的:

{
"Untitled": {
"0": [
1,
2,
"Title",
100,
100
],
"1": [
2,
7,
"Text box.<br> You can write here.",
100,
300
]
}
}

我会把它改成这样:

{
"Untitled": {
[
{
"name": "[You can set a name for the properties here]"
"a": 1,
"b": 2,
"text": "Title",
"x": 100,
"y": 100
},
{
"name": "[You can set a name for the properties here]"
"a": 2,
"b": 7,
"text": "Text box.<br> You can write here.",
"x": 100,
"y": 300
}
]
}
}

我意识到了我的错误!我需要做到<=键的长度,而不仅仅是<。

最新更新