1

I have array of grouped by scheduledOn data as below

[{
scheduledOn: "2020-02-05T00:00:00"
matches:
{id: 1, homeTeamName: "BLUE", homeTeamId: 1, homeScore: 1, awayTeamName: "Red", awayTeamId: 2, …}
{id: 2,homeTeamName: "Red", homeTeamId: 2, homeScore: 1, awayTeamName: "Yellow", awayTeamId: 3, …}
},
{
scheduledOn: "2020-01-06T00:00:00"
matches:
0: {id:3, homeTeamName: "BLUE", homeTeamId: 1, homeScore: 0, awayTeamName: "Yellow", awayTeamId: 3, …}
}]

I would like to return one matches object(Not an array) that match with selected id

Possible Solution I tried 1:

matches.map(match => match.matches
              .find(m => m.id === selectedId))

this returns:

 [{

    id: 1, homeTeamName: "BLUE", homeTeamId: 1, homeScore: 1, awayTeamName: "Red", awayTeamId: 2, …
    }]

but I want this to be just an object instead of array.

Possible Solution I tried 2:

matches.map(match => match.matches
              .filter(m => m.id === selectedId))
              .map(match => console.log(match

)

this returns:

 [
    {id: 1, homeTeamName: "BLUE", homeTeamId: 1, homeScore: 1, awayTeamName: "Red", awayTeamId: 2, …}
    ]

But this also returns as array.

Is there a way that I can have matched id object instead of array?

Thanks for all your help

3 Answers 3

2

You use find() twice

const matches = [{
scheduledOn: "2020-02-05T00:00:00",
matches:[
{id: 1, homeTeamName: "BLUE", homeTeamId: 1, homeScore: 1, awayTeamName: "Red", awayTeamId: 2 },
{id: 2,homeTeamName: "Red", homeTeamId: 2, homeScore: 1, awayTeamName: "Yellow", awayTeamId: 3 }]
},
{
scheduledOn: "2020-01-06T00:00:00",
matches:[{id:3, homeTeamName: "BLUE", homeTeamId: 1, homeScore: 0, awayTeamName: "Yellow", awayTeamId: 3 }]
}];


const selectedId =  3;
const required = matches.find(match => 
                  match.matches.find(m => m.id === selectedId));
console.log(required);

   // if you wanted nested object, add another `.find()`
   console.log(required .matches.find(m => m.id === selectedId));

Sign up to request clarification or add additional context in comments.

6 Comments

It returns {scheduledOn: "2020-02-05T00:00:00", matches: Array(2)} scheduledOn: "2020-02-05T00:00:00" matches: 0: {homeTeamName: "BLUE", homeTeamId: 1, homeScore: 1, awayTeamName: "Red", awayTeamId: 2, …} 1: {homeTeamName: "Red", homeTeamId: 2, homeScore: 1, awayTeamName:
i want only match data
This is what you asked in 1st version of question
I think I did not put a question right. Is there a way that I can only find one matches object?
@JakeKwon I added what you wanted. See the updated answer
|
1

You may flatten all matches properties into single array with flatMap() and search through it:

const src = [{"scheduledOn":"2020-02-05T00:00:00","matches":[{"id":1,"homeTeamName":"BLUE","homeTeamId":1,"homeScore":1,"awayTeamName":"Red","awayTeamId":2},{"id":2,"homeTeamName":"Red","homeTeamId":2,"homeScore":1,"awayTeamName":"Yellow","awayTeamId":3}]},{"scheduledOn":"2020-01-06T00:00:00","matches":[{"id":3,"homeTeamName":"BLUE","homeTeamId":1,"homeScore":0,"awayTeamName":"Yellow","awayTeamId":3}]}],
      itemWithId2 = src.flatMap(({matches}) => matches).find(({id}) => id==2)
      
console.log(itemWithId2)      

Though, you should bear in mind, it may fail (just like so many other cool things) in Microsoft browsers (at least until IE passed away).

If that is the case, you may use arr.reduce((r,{matches}) => r.concat(matches), []) instead of flatMap().

Though, when dealing with large arrays, nested finds, suggested by @Zohaibljaz would be more performant.

My implementation of it would be something unacceptably non-readable:

const src = [{"scheduledOn":"2020-02-05T00:00:00","matches":[{"id":1,"homeTeamName":"BLUE","homeTeamId":1,"homeScore":1,"awayTeamName":"Red","awayTeamId":2},{"id":2,"homeTeamName":"Red","homeTeamId":2,"homeScore":1,"awayTeamName":"Yellow","awayTeamId":3}]},{"scheduledOn":"2020-01-06T00:00:00","matches":[{"id":3,"homeTeamName":"BLUE","homeTeamId":1,"homeScore":0,"awayTeamName":"Yellow","awayTeamId":3}]}]

let objectHavingIdOf2 = {}

src.find(({matches}) => 
  matches.find(item => 
    item.id == 2 ? 
    (objectHavingIdOf2 = item, true) :
    false))
    
console.log(objectHavingIdOf2)    

1 Comment

You're most welcome. Take a look at extended version of my answer if performance does matter to process large arrays.
1

Here's a simple recursive function to help you with:

function findById(arr, id) {
    return arr.find(a => {
        if (a.matches && a.matches.length > 0) {
            return a.id === id ? true : findById(a.matches, id)
        } else {
            return a.id === id
        }
    })
}

// call this method with your data
findById(data, 1)

Comments

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.