Burak Taban
Burak Taban

Reputation: 429

Firebase get next child data

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

How do I compare the 'steps' with current child and the next child after?

Upvotes: 1

Views: 663

Answers (1)

Yakume
Yakume

Reputation: 161

You need first to convert the snap to an array.

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 : '';
    }
})

I don't know what you really want to do, but remember that every time that the condition is met, badge will be overwriten. If you need the first name that meets the condition, you need to use 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;
});

Upvotes: 1

Related Questions