如何重新开始对象中键的计数?
How to restart the numeration of the keys in a object?
What is the problem :
这是我存储在 this.state.jsondata
中的数据:
{
label{0:1,1:1}
prediction{0:0,1:1}
text{0:"abc",1:"def"}
}
这个在前 JSON.stringify(this.state.jsondata)
:
{"label":{"1":0,"2":0,"3":0,"4":0},"text":{"1":"aa","2":"bb,"3":"cc","4":"dd"},"prediction":{"1":2,"2":2,"3":2,"4":2}}
当我使用此代码删除一个元素时:
removeData = (keyToRemove) => {
// create a deep copy of the object
const clonedJsonData = JSON.parse(JSON.stringify(this.state.jsondata))
// now mutate this cloned object directly
Object.keys(clonedJsonData).forEach(key => delete clonedJsonData[key][keyToRemove])
// set the state
this.state.jsondata = clonedJsonData
this.state.jsondata
变为:
{
label{1:1}
prediction{1:1}
text{1:"def"}
}
而不是像 :
那样从 0 开始
{
label{0:1}
prediction{0:1}
text{0:"def"}
}
所以基本上我想将 key
设置回“0, 1, 2, 3..”
What have I tried :
我试图遍历我的数据,但是我循环遍历它的方式有问题:
removeData = (keyToRemove) => {
// create a deep copy of the object
const clonedJsonData = JSON.parse(JSON.stringify(this.state.jsondata))
// now mutate this cloned object directly
Object.keys(clonedJsonData).forEach(key => delete clonedJsonData[key][keyToRemove])
// reset keys value
let i = 0
Object.keys(clonedJsonData).forEach(function(key) {
Object.keys(clonedJsonData[key]).forEach(function(k) {
var newkey = i++;
clonedJsonData[key][k] = clonedJsonData[key][k];
delete clonedJsonData[key][k];
})
})
// set the state
this.state.jsondata = clonedJsonData
}
What is the expected output ?
预期输出为:
{
label{0:1}
prediction{0:1}
text{0:"def"}
}
如果你的键基本上代表值的索引,为什么不使用数组呢?
{ 标签:[1,1],预测:[0,1],文本:["abc","def"] }
如果您现在删除每个数组中的第一个条目,data.text[0] 将为“def”,依此类推...
编辑:
如果您不想转换数据,则必须小心,尤其是当您对整个内容进行解析和字符串化,然后遍历键时。之后它们不必按相同的顺序排列。
除了克隆对象、删除要删除的键然后更新所有键之外,您还可以手动复制对象并留下要删除的键
var data = {label: {0:0, 1:0}, prediction: {0:0, 1:1}, text: {0:'abc',1:'def'}};
console.log("Original Data");
console.log(data);
removeData = (data, keyToRemove) => {
var ret = {};
Object.keys(data).forEach(key=>{
ret[key] = {};
let idx = 0;
for (let dataIdx = 0; dataIdx < Object.keys(data[key]).length; dataIdx++) {
if (dataIdx.toString() !== keyToRemove) {
ret[key][idx.toString()] = data[key][dataIdx.toString()];
idx++;
}
}
});
return ret;
}
var reducedData = removeData(data,"0");
console.log("Reduced Data");
console.log(reducedData);
请注意,我没有在键上循环,所以我没有弄错顺序。
请注意,我还通过将数据对象作为参数移交来消除了原始函数的副作用。
这就是我的做法
removeData = (keyToRemove) => {
// create a deep copy of the object
const clonedJsonData = JSON.parse(JSON.stringify(this.state.jsondata))
// now mutate this cloned object directly
Object.keys(clonedJsonData).forEach(key => delete clonedJsonData[key][keyToRemove])
// reset keys value
let i = 0
Object.keys(clonedJsonData).forEach(function eachKey(key) {
i = 0
Object.keys(clonedJsonData[key]).forEach(function eachKey(kkey) {
var newkey = i++;
clonedJsonData[key][newkey] = clonedJsonData[key][kkey];
});
delete clonedJsonData[key][i];
});
// set the state
this.state.jsondata = clonedJsonData
}
What is the problem :
这是我存储在 this.state.jsondata
中的数据:
{
label{0:1,1:1}
prediction{0:0,1:1}
text{0:"abc",1:"def"}
}
这个在前 JSON.stringify(this.state.jsondata)
:
{"label":{"1":0,"2":0,"3":0,"4":0},"text":{"1":"aa","2":"bb,"3":"cc","4":"dd"},"prediction":{"1":2,"2":2,"3":2,"4":2}}
当我使用此代码删除一个元素时:
removeData = (keyToRemove) => {
// create a deep copy of the object
const clonedJsonData = JSON.parse(JSON.stringify(this.state.jsondata))
// now mutate this cloned object directly
Object.keys(clonedJsonData).forEach(key => delete clonedJsonData[key][keyToRemove])
// set the state
this.state.jsondata = clonedJsonData
this.state.jsondata
变为:
{
label{1:1}
prediction{1:1}
text{1:"def"}
}
而不是像 :
那样从 0 开始{
label{0:1}
prediction{0:1}
text{0:"def"}
}
所以基本上我想将 key
设置回“0, 1, 2, 3..”
What have I tried :
我试图遍历我的数据,但是我循环遍历它的方式有问题:
removeData = (keyToRemove) => {
// create a deep copy of the object
const clonedJsonData = JSON.parse(JSON.stringify(this.state.jsondata))
// now mutate this cloned object directly
Object.keys(clonedJsonData).forEach(key => delete clonedJsonData[key][keyToRemove])
// reset keys value
let i = 0
Object.keys(clonedJsonData).forEach(function(key) {
Object.keys(clonedJsonData[key]).forEach(function(k) {
var newkey = i++;
clonedJsonData[key][k] = clonedJsonData[key][k];
delete clonedJsonData[key][k];
})
})
// set the state
this.state.jsondata = clonedJsonData
}
What is the expected output ?
预期输出为:
{
label{0:1}
prediction{0:1}
text{0:"def"}
}
如果你的键基本上代表值的索引,为什么不使用数组呢? { 标签:[1,1],预测:[0,1],文本:["abc","def"] }
如果您现在删除每个数组中的第一个条目,data.text[0] 将为“def”,依此类推...
编辑:
如果您不想转换数据,则必须小心,尤其是当您对整个内容进行解析和字符串化,然后遍历键时。之后它们不必按相同的顺序排列。
除了克隆对象、删除要删除的键然后更新所有键之外,您还可以手动复制对象并留下要删除的键
var data = {label: {0:0, 1:0}, prediction: {0:0, 1:1}, text: {0:'abc',1:'def'}};
console.log("Original Data");
console.log(data);
removeData = (data, keyToRemove) => {
var ret = {};
Object.keys(data).forEach(key=>{
ret[key] = {};
let idx = 0;
for (let dataIdx = 0; dataIdx < Object.keys(data[key]).length; dataIdx++) {
if (dataIdx.toString() !== keyToRemove) {
ret[key][idx.toString()] = data[key][dataIdx.toString()];
idx++;
}
}
});
return ret;
}
var reducedData = removeData(data,"0");
console.log("Reduced Data");
console.log(reducedData);
请注意,我没有在键上循环,所以我没有弄错顺序。 请注意,我还通过将数据对象作为参数移交来消除了原始函数的副作用。
这就是我的做法
removeData = (keyToRemove) => {
// create a deep copy of the object
const clonedJsonData = JSON.parse(JSON.stringify(this.state.jsondata))
// now mutate this cloned object directly
Object.keys(clonedJsonData).forEach(key => delete clonedJsonData[key][keyToRemove])
// reset keys value
let i = 0
Object.keys(clonedJsonData).forEach(function eachKey(key) {
i = 0
Object.keys(clonedJsonData[key]).forEach(function eachKey(kkey) {
var newkey = i++;
clonedJsonData[key][newkey] = clonedJsonData[key][kkey];
});
delete clonedJsonData[key][i];
});
// set the state
this.state.jsondata = clonedJsonData
}