Waterman1 Waterman1 - 2 months ago 6
Javascript Question

Finding a value of a property in a JavaScript object inside an array

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: "ed@ed.com"
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')}

Answer

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