skip to Main Content

so i have a bad bad logic for this assignment, i need to filter 2 Dimension Array that contains the value of string that input on 2nd array.

So this is the example of the 2-Dim Array

const roles = [ ['roles', 'admin', 'write'] ['roles' ,'admin', 'read'] ['roles', 'member', 'read']]

I need to filter the array that only containts ‘member’ and the result is like this

const result = ['roles', 'member', 'read']

How can i achieve this ?

2

Answers


  1. Chosen as BEST ANSWER

    Had a simple answer by drinking a water and trying get my brain straight hahahaha

    this is the answer

    const result = roles.filter(
      (dt) => dt[1] === 'member'
    );
    

  2. const roles = [ ['roles', 'admin', 'write'], ['roles' ,'admin', 'read'] ,['roles', 'member', 'read']]
    const answer = roles.find(arr => arr.some((e) => e === "member"));
    console.log(answer)

    If you want to return multiple arrays consisting the same string say "admin"
    use filter inplace of find in the above code

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search