1

How can I find something through some arrays that also contain an array? To be more precisely:

enter image description here

And I want to return from the coaches array, the id(within the coaches) that matches the username. What I've tried:

  if (!(args['coach'].value === '') && (args['coach'].value !== null)) {
    coachId = this.items.find(x => x.username === args.coach.value).id;
  }

Basically this.items is what I've console.log before. Now it gives me undefined.

Has someone a fix for this? Thank you very much.

[
   {
      "id":584,
      "name":"Name",
      "coaches":[
         {
            "id":8587,
            "username":"test"
         },
         {
            "id":8589,
            "username":"test1"
         }
      ]
   },
   {
      "id":587,
      "name":"O1",
      "coaches":[

      ]
   }
]

And let s say I want to return the id 8587 when searching for the name test.

9
  • can you paste this.items JSON Commented May 20, 2019 at 16:12
  • @Sajeetharan edited the question. Commented May 20, 2019 at 16:15
  • 8587 for example if I am searching with the name test. Commented May 20, 2019 at 16:20
  • data.map(it => it.coaches.find(it2 => it2.username == "test")).filter(it => !!it).map(it=> it.id) Commented May 20, 2019 at 16:22
  • Thank you for your response. Commented May 20, 2019 at 16:25

4 Answers 4

3

Combine map and find:

const array = [
  [{a1: 1},
   {a2: 2}
  ],
  [{a1: 1},
   {a2: 2},
   {a3: 3}]
];

const element = array.map(innerArray => {
  const found = innerArray.find(el => el.a1 === 1);
  if (found) return found.a1;
  return null;
}).find(el => el !== null);

console.log(element) // 1
Sign up to request clarification or add additional context in comments.

2 Comments

I got the error message: innerArray.find is not a function
You have to adjust it for your object
3

For finding multiple matches do as follows:

const data = [{
    "id": 584,
    "name": "Name",
    "coaches": [{
        "id": 8587,
        "username": "test"
      },
      {
        "id": 8589,
        "username": "test1"
      }
    ]
  },
  {
    "id": 587,
    "name": "O1",
    "coaches": [

    ]
  }
];

const usernameToSearch = 'test1';

const foundCoachIds = data
  .reduce((acc, curr) => {

    // Destructure the coaches property first
    const {
      coaches,
      ...rest
    } = curr;

    // Check if any username matches the coach
    const foundMatches = coaches.filter(x => x.username === usernameToSearch);

    // If there is any found match push into accumulator
    if (foundMatches.length) {
      for (const foundMatch of foundMatches) {
        if (acc.indexOf(foundMatch.id) === -1) {
          acc.push(foundMatch.id);
        }
      }
    }

    return acc;
  }, []);

console.log(foundCoachIds);

6 Comments

Nice solution, you can just use find in place of filter and drop the || [] as much as the [0]
Does this deal with more than one hit? I dont think it does. Not the best answer imo
@Uzer what do you mean by one hit ? I didnt get ?
reg. this line if (foundCoach[0] && foundCoach[0].id), what if the username is found in second element of coaches array?
@Uzer modified my answer
|
2
let y = this.items.filter(o => o.coaches.some(e => e.username === 'test'))[0]
            .coaches.filter(e=>e.username === 'test')[0].id;

console.log(y);

Comments

2
const data = [
   {
      "id":584,
      "name":"Name",
      "coaches":[
         {
            "id":8587,
            "username":"test"
         },
         {
            "id":8589,
            "username":"test1"
         }
      ]
   },
   {
      "id":587,
      "name":"O1",
      "coaches":[

      ]
   }
];   

For outer id

data.map(it => {return !!it.coaches.find(it2 => it2.username == "test") ? it.id : null}).filter(it=>!!it)

evaluates to [584]

For inner (coaches) id:

data.map(it => it.coaches.find(it2 => it2.username == "test")).filter(it => !!it).map(it=> it.id) 

returns [8587]

Just need to take the first item from these to get your answer.

3 Comments

For some reason it gives me [number];
Yes, the id within the coaches array.
that was my first answer in the comments... nevermind

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.