使用forEach循环从Firebase创建指定键和值的对象数组

时间:2017-04-04 00:59:54

标签: arrays node.js firebase foreach firebase-realtime-database

我需要从Firebase返回一个对象数组,然后我可以操作它。

下面的代码记录了许多递增长度的数组。我只需要最后一个数组,该数组具有我正在引用的子项中可用数据的对象总数。一旦我有这个数组,我需要操纵它。我正在处理法庭数据。 weeklyData只是在他们出庭前x晚上被关押的人数。

var weeklyNightsHeld = db.ref(`nightsHeld/weeklyNightsHeld`);
var array = [];

weeklyNightsHeld.on("child_added", function(snapshot) {

  snapshot.forEach(function(data) {
    var weeklyData = new Object();
    weeklyData.days = data.key;
    weeklyData.people = data.val();

    array.push(weeklyData)

    console.log(array);
});
  });

以下是我的Firebase数据:

 {
  "nightsHeld" : {
    "weeklyNightsHeld" : {
      "-KgmbMAtaK7AvkaPWv-K" : {
        "1" : 6,
        "5" : 8,
        "6" : 12
      },
      "-Kgmbt-TbnZ8CRHamHnR" : {
        "2" : 8,
        "5" : 8,
        "6" : 12
      },
      "-Kgmc8U6LIBBXYBvBfu_" : {
        "5" : 8,
        "6" : 12,
        "7" : 10
      },
      "-KgmcIxN4CJTROqn3Ihu" : {
        "5" : 8,
        "6" : 12
      }
    }
  }
}

目前,console.log(array);记录以下增加长度的数组:

 [ { days: '1', people: 6 } ]
[ { days: '1', people: 6 }, { days: '5', people: 8 } ]
[ { days: '1', people: 6 },
  { days: '5', people: 8 },
  { days: '6', people: 12 } ]
[ { days: '1', people: 6 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '2', people: 8 } ]
[ { days: '1', people: 6 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '2', people: 8 },
  { days: '5', people: 8 } ]
[ { days: '1', people: 6 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '2', people: 8 },
  { days: '5', people: 8 },
  { days: '6', people: 12 } ]
[ { days: '1', people: 6 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '2', people: 8 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '5', people: 8 } ]
[ { days: '1', people: 6 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '2', people: 8 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '5', people: 8 },
  { days: '6', people: 12 } ]
[ { days: '1', people: 6 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '2', people: 8 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '7', people: 10 } ]
[ { days: '1', people: 6 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '2', people: 8 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '7', people: 10 },
  { days: '5', people: 8 } ]
[ { days: '1', people: 6 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '2', people: 8 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '7', people: 10 },
  { days: '5', people: 8 },
  { days: '6', people: 12 } ]

我只需要检索一个数组console.log(array);记录的最后一个数组

[ { days: '1', people: 6 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '2', people: 8 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '5', people: 8 },
  { days: '6', people: 12 },
  { days: '7', people: 10 },
  { days: '5', people: 8 },
  { days: '6', people: 12 } ]

然后我需要将每个“days”键的总人数加上一个共同的值。 例如,我想要这个新数组:

 [{ days: '1', people: 6 },
  { days: '5', people: 32 },
  { days: '6', people: 48 },
  { days: '2', people: 8 },
  { days: '7', people: 10 }]

我在Node.js中使用网络管理员来访问我的Firebase实时数据库。

1 个答案:

答案 0 :(得分:0)

第一部分的答案是使用value代替child_addedchild_added异步地遍历孩子,所以我们无法知道我们什么时候结束。 value将立即检索所有数据,然后我们应该使用两个forEach循环来检索我们的数据。

weeklyNightsHeld.on("value", function(snapshot) {
  let array = []

  snapshot.forEach(childSnapshot => {

    childSnapshot.forEach(data => {
      var weeklyData = {}
      weeklyData.days = data.key
      weeklyData.people = data.val()
      array.push(weeklyData)
    })
  })
  console.log(array) // We should have correct data here
})

关于获取数组中天数总和的部分,我们可以使用reduce上的漂亮Array函数。

const daysReduced = array.reduce((a, b) => {
  if (Object.keys(a).length === 0) {
    a[b.days] = b.people
    return a
  } else {
    if (a[b.days])
      a[b.days] += b.people
    else
      a[b.days] = b.people
    return a
  }
}, {})

我已经在ES6中编写了我的代码,但如果需要的话,翻译成CommonJS应该不难。