0

I have managed to pull some users back from my firebase database and am outputting them into the console

[Object, Object, Object, Object, Object]

Each object has:

gender: "male"
liked: "liked"
name: "ed"
not_liked: "liked"
username: "[email protected]"
useruid: "HSRcN6PuylXbv87kSWxKVtWffaI3"

how can I go through the array and say if the useruid with a value HSRcN6PuylXbv87kSWxKVtWffaI3 then console.log() the associated name. In this case ed

my pseudo attempt :

if (Object.gender.includes('male')) {
    console.log('yes')
} 
else {
    console.log('nah')
}
3
  • 2
    Use a loop and an if-statement? Please show us what you tried. Commented Sep 9, 2016 at 14:02
  • updated code @Bergi Commented Sep 9, 2016 at 14:05
  • But where's the loop, what is Object? And why are you accessing gender when you say you're interested in the useruid? Commented Sep 9, 2016 at 14:10

2 Answers 2

1

Considering you are storing array in dataArray variable in javascript.

for (i = 0; i < dataArray.length; i++) { 
    if(dataArray[i].useruid=="HSRcN6PuylXbv87kSWxKVtWffaI3"){
        console.log(dataArray[i].username);
    }
}

It will print username on console for userid=HSRcN6PuylXbv87kSWxKVtWffaI3

Sign up to request clarification or add additional context in comments.

1 Comment

No problem. Let me know if there is any issue
0

Simply loop through your array and throw a log when a match has been found.

for(var i = 0; i < arr.length; i++) {
     if(arr[i].useruid === "HSRcN6PuylXbv87kSWxKVtWffaI3") {
          console.log(arr[i].username);
     }
}

As for your pseudo attempt:

for(var i = 0; i < arr.length; i++) {
     if(arr[i].gender === "male") {
          console.log("User is male");
     }
     else {
          console.log("User is female");
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.