firebase db splice like操作

问题描述

如果我的数据库中有与此类似的数据

/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

解决方法

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);