Javascript - filter array of objects based on key's value existing in a separate array

时间:2018-04-18 18:16:33

标签: javascript ecmascript-6

Fairly new to Javascript as I'm working on my first app, I am coming over from R where though data manipulations (with dplyr or even base R) become very easy, but I am struggling with this currently. I have the following data:

var teamsA = ['team1', 'team2', 'team3'];
var teamsB = ['team4', 'team5', 'team6'];

var teamgroup = A;

var myData = [
    {player: "Joe", team: "team1"},
    {player: "Tom", team: "team3"},
    {player: "Red", team: "team2"},
    {player: "Smi", team: "team5"},
    {player: "Bib", team: "team6"},
    {player: "Cat", team: "team2"},
    {player: "Dan", team: "team3"},
    {player: "Jim", team: "team1"}
]

With the data shown, the question is fairly simple: I would like to filter myData based on the team existing in whichever array is determined by the teamgroup variable. ie:

if(teamgroup == "A") { 
    myData.filter(team in teamsA) 
} else {
    myData.filter(team in teamsB)
}

...not quite sure how to do so with javascript. Prefer to use the new ES6 stuff. Thanks!

4 个答案:

答案 0 :(得分:2)

您可以使用一个功能,该功能需要玩家和球队阵列并使用Array#includes进行过滤。



function getTeam(players, teams) {
    return players.filter(({ team }) => teams.includes(team));
}

var teamsA = ['team1', 'team2', 'team3'],
    teamsB = ['team4', 'team5', 'team6'],
    myData = [{ player: "Joe", team: "team1" }, { player: "Tom", team: "team3" }, { player: "Red", team: "team2" }, { player: "Smi", team: "team5" }, { player: "Bib", team: "team6" }, { player: "Cat", team: "team2" }, { player: "Dan", team: "team3" }, { player: "Jim", team: "team1" }];

console.log(getTeam(myData, teamsA));
console.log(getTeam(myData, teamsB));

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:1)

你可以像你想要的那样使用filter

var teamsA =
  [ 'team1', 'team2', 'team3' ]
  
var teamsB =
  [ 'team4', 'team5', 'team6' ]

var myData =
  [ {player: "Joe", team: "team1"}
  , {player: "Tom", team: "team3"}
  , {player: "Red", team: "team2"}
  , {player: "Smi", team: "team5"}
  , {player: "Bib", team: "team6"}
  , {player: "Cat", team: "team2"}
  , {player: "Dan", team: "team3"}
  , {player: "Jim", team: "team1"}
  ]

const A =
  myData.filter (p => teamsA.includes (p.team))
  
const B =
  myData.filter (p => teamsB.includes (p.team))
  
console.log (A) // Joe Tom Red Cat Dan Jim
console.log (B) // Smi Bib

虽然做一个函数会更好

const playersForTeams = (players, teams) =>
  players.filter (p => teams.include (p.team))

const A =
  playersForTeams (myData, teamsA)

const B =
  playersForTeams (myData, teamsB)

console.log (A) // Joe Tom Red Cat Dan Jim
console.log (B) // Smi Bib

答案 2 :(得分:-1)

var teamsA = ['team1', 'team2', 'team3'];
var teamsB = ['team4', 'team5', 'team6'];

// What's the purpose of the below line?
// var teamgroup = A;


var myData = [
    {player: "Joe", team: "team1"},
    {player: "Tom", team: "team3"},
    {player: "Red", team: "team2"},
    {player: "Smi", team: "team5"},
    {player: "Bib", team: "team6"},
    {player: "Cat", team: "team2"},
    {player: "Dan", team: "team3"},
    {player: "Jim", team: "team1"}
]

console.log(myData.filter(item => teamsA.indexOf(item.team) !== -1))

答案 3 :(得分:-1)



head