2

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!

1
  • where is the connection between A and the two variables teamsA and teamsB? wound an object with properties A and B fit better for the selection for filtering? Commented Apr 18, 2018 at 18:29

4 Answers 4

2

You could use a function which takes players and the teams array and filter with 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; }

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

Comments

1

You can use filter much like you intend to

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

Though it would be better to make a function

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

1 Comment

@TheIncorrigible1 thanks for the remark, ha! I'm all about reminding programmers who's the boss: you, not your language.
-1

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

if(teamgroup == 'A'){
  myData = myData.filter((player)=> teamsA.includes(player.team))
} else {
  myData = myData.filter((player)=> teamsB.includes(player.team))

}

console.log(myData)

1 Comment

going to check all the solutions but this one looks particularly clean / what I am looking for.
-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))

1 Comment

in my React App, teamgroupA is passed as a prop to the component that does this work.

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.