如何过滤属于数组值的对象?

时间:2017-10-20 22:15:34

标签: javascript arrays loops object filter

鉴于以下数据

[
  {
    "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
      }
    ]
  }
]

如何过滤掉对象,以便我只显示当天播放的曲棍球比赛。基本上我需要返回相同的对象数组,但只有在游戏键= sport: hockey

时才会显示该对象

我知道我只能在数组上运行filter方法,但我无法弄清楚如何遍历数组内部的对象并再次返回整个对象。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

尝试:

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"
            }
        ]
    }
]

我认为你想要的是什么。

答案 1 :(得分:0)

我认为以下代码可以解决这个问题:

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"))

答案 2 :(得分:0)

让我们看一个数组:-

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

现在,我要过滤大于或等于90的标记,代码将如下所示。

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将展开student,然后将其用marks key覆盖filtered marks value

如果您不使用传播运算符,

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

然后,您将仅获得过滤后的值,而不是尚未应用过滤器的值。