0

so i am making a following / followers component in my react code and using redux to manage the state, so lets say a user follows another user and my following model in my data base might look like this users following model

following: [
{
_id: "1213231235t34",
user: "242545367675445666",
username: "john",
email: "john@d.com"
},
{
_id: "625675676552",
user: "876869897976566",
username: "lenon",
email: "john@d.com"
},
{
_id: "1213231235t34",
user: "34536655344365654",
username: "mike",
email: "john@d.com"
},
{
_id: "1213231235t34",
user: "FINDTHIS",
username: "crease",
email: "john@d.com"
}
]

how can i find if the user(FIND THIS) is in the array and do something if it is found

for example the following model is like this when a user follows people

now if i go on the page of crease(THE LAST OBJECT IN THE ARRAY) i do not want to see a follow button, rather i want to see an unfollow button because he is already in my array of the users i am currently following. so how can i loop through the array and find the field of user(FINDTHIS)for the crease object... bascially i am asking how can i find if i am following crease... mind you i am using react so es6 syntax if you could help that would be wonderful... THANKS! if you do not understand the question please kindly ask for more info

1 Answers1

0

When I understood your question correctly this should help you:

const findSpecificUser = ArrayName.map((user) => {
    if(user.user === 'FINDTHIS'){
        //display or not display the ui elements
    }
})
Dominik S.
  • 81
  • 5
  • yeah, thanks i just figured it out myself, such a noob question lmao, thanks anyways –  Jun 20 '20 at 19:57
  • I would suggest you to take a look at the MDN Documentation for Array-Methods, as these kind of problems will occur more often. – Dominik S. Jun 20 '20 at 19:59