Home > Back-end >  Find and match string in array object and then sort by object value. React and Javascript
Find and match string in array object and then sort by object value. React and Javascript

Time:07-08

i have an array of nested objects that give each user a permission.

 const UserList = [{
           0: {
                users: {
                "email": "[email protected]"
                },
                permission: {
                "id": 1,
                "access": "Edit"
                }
              },
            1: {
                users: {
                "email": "[email protected]"
                },
                permission: {
                "id": 2,
                "access": "Read Only"
                }
              },
            2: {
                users: {
                "email": "[email protected]"
                },
                permission: {
                "id": 1,
                "access": "Edit"
                }
              },
            }]

My problem: I want to be able to match a email string to the email in the object and then grab object with the access "read only'. This is all to disable a button. So if the current user's email matches one in the object and the access equals "read only" then pull it out. Im not sure if i want to create a function/prop for this condition but the disable button is in another file.

So lets say this is my email

const myEmail = [email protected]. How do i compare it to UserList and create that condition above. Then transfer it to a button in another file <button disabled={solution goes here}></button>

Edit: I want to find the object that has the current users email, then if that object's access = read only, then disable.

Maybe a .include(myEmail)? Not sure

Thanks for your help!

CodePudding user response:

You can use Object.values(UserList) to get an array with just the values (the user object in this case). Then you can use that list to do your rendering (seems you are using react), so you can just map over the list, and then use the information to check whether a value is `Read Only" etc.

in your HTML, that looks something like: <button disabled={userObj.permissions.access !== "Read Only"}>

I am not quite following the question about matching email strings.

  • Related