如果我数据库中有类似如下的数据
/values:
value1: somevalue1
value2: somevalue2
specialValue1 : 1
specialValue2 : 2 //to be "removed" and rest values moved
specialValue3 : 3
specialValue4 : 4我如何做类似“数组拼接”的操作,去掉specialValue2并移动剩下的部分,所以我得到:
value1: somevalue1
value2: somevalue2
specialValue1 : 1
specialValue2 : 3
specialValue3 : 4发布于 2020-08-11 07:26:55
Firebase实时数据库中没有类似拼接的操作。您必须读取所有数据,在应用程序代码中对其进行修改,然后将其写回数据库。
在纯JavaScript中,对象操作将如下所示:
var input = {
specialValue1 : 1,
specialValue2 : 2, //to be "removed" and rest values moved
specialValue3 : 3,
specialValue4 : 4
};
var keys = Object.keys(input);
var offset = 0;
var output = {};
keys.forEach((key, index) => {
if (input[key] != 2) {
output[keys[index-offset]] = input[key];
}
else {
offset = offset + 1;
}
});
console.log(output);
https://stackoverflow.com/questions/63349192
复制相似问题