Firebase获取下一个孩子的数据
问题描述:
badgeRef.once('value', badges);
function badges(snapshot) {
var badge = null;
snapshot.forEach(childSnapshot => {
const data = childSnapshot.val();
if(data.steps < stpes && NEXT_CHILD.steps > steps){
console.log('heey')
badge = data.name;
}
});
}
如何比较“步骤”与当前孩子和下一个孩子之后?Firebase获取下一个孩子的数据
答
您需要首先将捕捉转换为数组。
let arr = [];
snapshot.forEach(childSnapshot => {
arr.push(childSnapshot.val());
});
let badge;
arr.forEach((data, index) => {
if (arr.length > index + 1) {
badge = data.steps < steps && arr[index + 1].steps > steps ? data.name : '';
}
})
我不知道你真正想做的事,但请记住,每一个条件满足时,标识将被自动覆盖。如果您需要符合条件的名字,则需要使用some。
arr.some((data, index) => {
if (arr.length > index + 1) {
if (data.steps < steps && arr[index + 1].steps > steps) {
badge = data.name;
return true;
}
}
return false;
});