javascript 如何找到这两个属性为字符串和数组的对象的重叠部分?

eyh26e7m  于 2023-01-04  发布在  Java
关注(0)|答案(1)|浏览(112)

我有两个代表棒球运动员的对象,我想看看他们是否曾经在同一个队打过球,他们所效力的每支球队都有一个球队对象,属性是名字和他们在那支球队效力的年数,我想比较这两个对象,看看是否有重叠。

{
    "_id": "/players/h/hunteto01.shtml",
    "url": "/players/h/hunteto01.shtml",
    "name": "Torii Hunter",
    "image": "https://www.baseball-reference.com/req/202108020/images/headshots/7/79f9873b_br.jpg",
    "teams": [{
        "name": "MIN",
        "years": [1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2015]
    }, {
        "name": "LAA",
        "years": [2008, 2009, 2010, 2011, 2012]
    }, {
        "name": "DET",
        "years": [2013, 2014]
    }],
    "searchName": "torii hunter"
}

{
    "_id": "/players/m/mauerjo01.shtml",
    "url": "/players/m/mauerjo01.shtml",
    "name": "Joe Mauer",
    "image": "https://www.baseball-reference.com/req/202108020/images/headshots/4/43c69595_mlbam.jpg",
    "teams": [{
        "name": "MIN",
        "years": [2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018]
    }],
    "searchName": "joe mauer"
}

重叠发生在MIN 2004, 2005, 2006, 2007, 2015,我只需要返回一个布尔值,我实际上不需要值或任何东西,我怎么在javascript中完成这个。

nlejzf6q

nlejzf6q1#

你能做这样的事吗?

const playerOne = {
  _id: "/players/h/hunteto01.shtml",
  url: "/players/h/hunteto01.shtml",
  name: "Torii Hunter",
  image:
    "https://www.baseball-reference.com/req/202108020/images/headshots/7/79f9873b_br.jpg",
  teams: [
    {
      name: "MIN",
      years: [
        1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2015,
      ],
    },
    {
      name: "LAA",
      years: [2008, 2009, 2010, 2011, 2012],
    },
    {
      name: "DET",
      years: [2013, 2014],
    },
  ],
  searchName: "torii hunter",
};

const playerTwo = {
  _id: "/players/m/mauerjo01.shtml",
  url: "/players/m/mauerjo01.shtml",
  name: "Joe Mauer",
  image:
    "https://www.baseball-reference.com/req/202108020/images/headshots/4/43c69595_mlbam.jpg",
  teams: [
    {
      name: "MIN",
      years: [
        2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
        2016, 2017, 2018,
      ],
    },
  ],
  searchName: "joe mauer",
};

/**
 * This returns a boolean value if two players have played on the same team in the same year
 * @param playerOne
 * @param playerTwo
 * @returns boolean
 **/
function sameTeamSameYear(playerOne, playerTwo) {
  const playerOneTeams = playerOne.teams;
  const playerTwoTeams = playerTwo.teams;

  for (const playerOneTeam of playerOneTeams) {
    for (const playerTwoTeam of playerTwoTeams) {
      if (playerOneTeam.name === playerTwoTeam.name) {
        for (const playerOneYear of playerOneTeam.years) {
          for (const playerTwoYear of playerTwoTeam.years) {
            if (playerOneYear === playerTwoYear) {
              return true;
            }
          }
        }
      }
    }
  }

  return false;
}

console.log(sameTeamSameYear(playerOne, playerTwo));

或者,如果你喜欢一个可能更干净的方法,你也可以这样做:

const playerOne = {
  _id: "/players/h/hunteto01.shtml",
  url: "/players/h/hunteto01.shtml",
  name: "Torii Hunter",
  image:
    "https://www.baseball-reference.com/req/202108020/images/headshots/7/79f9873b_br.jpg",
  teams: [
    {
      name: "MIN",
      years: [
        1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2015,
      ],
    },
    {
      name: "LAA",
      years: [2008, 2009, 2010, 2011, 2012],
    },
    {
      name: "DET",
      years: [2013, 2014],
    },
  ],
  searchName: "torii hunter",
};

const playerTwo = {
  _id: "/players/m/mauerjo01.shtml",
  url: "/players/m/mauerjo01.shtml",
  name: "Joe Mauer",
  image:
    "https://www.baseball-reference.com/req/202108020/images/headshots/4/43c69595_mlbam.jpg",
  teams: [
    {
      name: "MIN",
      years: [
        2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
        2016, 2017, 2018,
      ],
    },
  ],
  searchName: "joe mauer",
};

/**
 * This returns a boolean value if two players have played on the same team in the same year (possibly a cleaner approach)
 * @param playerOne
 * @param playerTwo
 * @returns boolean
 **/
function sameTeamSameYear(playerOne, playerTwo) {
  const playerOneTeams = playerOne.teams.map((team) => team.name);
  const playerTwoTeams = playerTwo.teams.map((team) => team.name);

  const playerOneYears = playerOne.teams.reduce((acc, team) => {
    return [...acc, ...team.years];
  }, []);

  const playerTwoYears = playerTwo.teams.reduce((acc, team) => {
    return [...acc, ...team.years];
  }, []);

  const sameTeam = playerOneTeams.some((team) => playerTwoTeams.includes(team));
  const sameYear = playerOneYears.some((year) => playerTwoYears.includes(year));

  return sameTeam && sameYear;
}

console.log(sameTeamSameYear(playerOne, playerTwo));

相关问题