Blake
Blake

Reputation: 623

How do I filter an object that is part of an array value?

Given the following data

[
  {
    "date": "2017-10-04",
    "games": [
      {
        "notes": "Game was played",
        "time": "2017-10-04T20:24:30+00:00",
        "sport": "hockey",
        "owner": "steve",
        "players": "10",
        "game_id": 1
      },
      {
        "notes": "Game was played",
        "time": "2017-10-04T12:35:30+00:00",
        "sport": "lacrosse",
        "owner": "steve",
        "players": "6",
        "game_id": 2
      },
      {
        "notes": "Game was played",
        "time": "2017-10-04T10:12:30+00:00",
        "sport": "hockey",
        "owner": "henry",
        "players": "10",
        "game_id": 4
      }
    ]
  },
  {
    "date": "2017-10-14",
    "games": [
      {
        "notes": "Game was played",
        "time": "2017-10-14T20:32:30+00:00",
        "sport": "hockey",
        "owner": "steve",
        "players": "4",
        "game_id": 3
      },
      {
        "notes": "Game was played",
        "time": "2017-10-14T20:34:30+00:00",
        "sport": "soccer",
        "owner": "john",
        "players": "12",
        "game_id": 5
      }
    ]
  }
]

how do I filter out the objects so that I only show the hockey games played on that date. Essentially I need the same array of objects back, but the object should only be shown if the games key = sport: hockey

I know I can only run the filter method on arrays, but I can't figure out how to loop over the object inside of the array and return the whole object again. Any help would be greatly appreciated.

Upvotes: 0

Views: 92

Answers (3)

Nitesh Ranjan
Nitesh Ranjan

Reputation: 1329

Let's take an array :-

let students = [
  {"Name": "Priya","marks": [50, 60, 70, 80, 90]},
  {"Name": "Ankita","marks": [80, 90, 95]}
]

Now, I want to filter marks greater than or equal to 90, The code will look like this.

students.map((student) => {
  return {...student, 
          marks: student.marks.filter((mark) => mark >= 90)
         }
}) 
// Result will be [{"Name": "Priya", "marks": [90]}, {"Name": "Ankita","marks": [90,95]}]

Spread operator will expand student and then it will override the marks key with filtered marks value.

If you will not use spread operator,

students.map((student) => student.marks.filter((mark) => mark >= 90))
// Result will be [[90],[90]]

Then you will get only the filtered values and not the values on which you haven't applied the filter.

Upvotes: 0

user2887596
user2887596

Reputation: 643

I think the following code should do the trick:

var x=[
{
"date": "2017-10-04",
"games": [
  {
    "notes": "Game was played",
    "time": "2017-10-04T20:24:30+00:00",
    "sport": "hockey",
    "owner": "steve",
    "players": "10",
    "game_id": 1
  },
  {
    "notes": "Game was played",
    "time": "2017-10-04T12:35:30+00:00",
    "sport": "lacrosse",
    "owner": "steve",
    "players": "6",
    "game_id": 2
  },
  {
    "notes": "Game was played",
    "time": "2017-10-04T10:12:30+00:00",
    "sport": "hockey",
    "owner": "henry",
    "players": "10",
    "game_id": 4
  }
]
},
{
"date": "2017-10-14",
"games": [
  {
    "notes": "Game was played",
    "time": "2017-10-14T20:32:30+00:00",
    "sport": "hockey",
    "owner": "steve",
    "players": "4",
    "game_id": 3
  },
  {
    "notes": "Game was played",
    "time": "2017-10-14T20:34:30+00:00",
    "sport": "soccer",
    "owner": "john",
    "players": "12",
    "game_id": 5
  }
]
}]


function filterByDate (data, date){

   return data.filter(function(entry){
     return sameDay(new Date(entry.date), date)

   })
}

function filterBySport(data, sport){
   data.forEach(function(entry){
     entry.games=entry.games.filter(function(entry2){
        return entry2.sport===sport
     })
   })
   return data
 }

 function sameDay(date1, date2){ //helper function to check for date equality
   return date1.getFullYear()===date2.getFullYear() &&
          date1.getMonth()===date2.getMonth() &&
         date1.getDay()===date2.getDay() 
 }

 function myFilter(data, date, sportType){ // the final function you have to call
    return filterBySport(filterByDate(data, date), sportType)
 }

 console.log(myFilter(x, new Date("2017-10-04"), "hockey"))

Upvotes: 0

CRice
CRice

Reputation: 32186

Try:

const filtered = yourArray.map(item => ({...item, games: item.games.filter(game => game.sport === 'hockey')})

// When run, this produces:
[
    {
        "date": "2017-10-04", 
        "games": [
            {
                "game_id": 1, 
                "notes": "Game was played", 
                "owner": "steve", 
                "players": "10", 
                "sport": "hockey", 
                "time": "2017-10-04T20:24:30+00:00"
            }, 
            {
                "game_id": 4, 
                "notes": "Game was played", 
                "owner": "henry", 
                "players": "10", 
                "sport": "hockey", 
                "time": "2017-10-04T10:12:30+00:00"
            }
        ]
    }, 
    {
        "date": "2017-10-14", 
        "games": [
            {
                "game_id": 3, 
                "notes": "Game was played", 
                "owner": "steve", 
                "players": "4", 
                "sport": "hockey", 
                "time": "2017-10-14T20:32:30+00:00"
            }
        ]
    }
]

Which I think is what you want.

Upvotes: 1

Related Questions