0

I have a list of gameData and used athletes. I need to make a new array that only contains Athlete from gameData that is not in usedAthletes. I've honestly looked all over, tried lodash and various things over the past day or so. Any advise would be great. If there are ES6 methods that work it would be cool to know that too :)

const gameData = [
        {Athlete: "Peyton Manning", Img: "url"},
        {Athlete: "Tony Hawk", Img: "url"},
        {Athlete: "Tom Brady", Img: "url"},
        {Athlete: "Usain Bolt", Img: "url"},
        {Athlete: "Kevin Durant", Img: "url"},
        {Athlete: "Cristiano Ronaldo", Img: "url"},
        {Athlete: "Michael Phelps", Img: "url"},
        {Athlete: "Conor McGregor", Img: "url"},
        {Athlete: "Phil Mickelson", Img: "url"},
        {Athlete: "Stephen Curry", Img: "url"},
        {Athlete: "Rory McIlroy", Img: "url"},
        {Athlete: "Mike Trout", Img: "url"},
        {Athlete: "Danica Patrick", Img: "url"},
        {Athlete: "Drew Brees", Img: "url"},
        {Athlete: "Carmelo Anthony", Img: "url"},
        {Athlete: "Ryan Lochte", Img: "url"},
        {Athlete: "Eli Manning", Img: "url"},
        {Athlete: "Chris Paul", Img: "url"}
]

const usedAthletes = ["Peyton Manning", "Tony Hawk", "Tom Brady"];

gameData.forEach( x => {
  const gameDataNamesOnly = x.Athlete;
  newAnswerlist = [];
  usedAthletes.forEach( item => {
    if(gameDataNamesOnly != item){
        //I was trying to push to newAnswerList here but could get access to gameDataNamesOnly correctly or something.
    }
  })
  console.log(newAnswerlist)
})

5 Answers 5

1

Use filter followed by map,

const newAnswerlist = gameData
.filter(data => usedAthletes.indexOf(data.Athlete) !== -1)
.map(data => data.Athlete);

Use only filter if you want the img information too,

const newAnswerlist = gameData
.filter(data => usedAthletes.indexOf(data.Athlete) !== -1);
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this but when I console log newAnswerlist it returns an empty array.
@SirFry There was a typo in Athelete, made it Athlete
lul I missed that too, sorry at work right now. Thanks for the help!
1

You could use Array#filter with Array#includes and exclude the usedAthletes.

You solution does not work, because you initialize the result array in every iteration.

const
    gameData = [{ Athlete: "Peyton Manning", Img: "url" }, { Athlete: "Tony Hawk", Img: "url" }, { Athlete: "Tom Brady", Img: "url" }, { Athlete: "Usain Bolt", Img: "url" }, { Athlete: "Kevin Durant", Img: "url" }, { Athlete: "Cristiano Ronaldo", Img: "url" }, { Athlete: "Michael Phelps", Img: "url" }, { Athlete: "Conor McGregor", Img: "url" }, { Athlete: "Phil Mickelson", Img: "url" }, { Athlete: "Stephen Curry", Img: "url" }, { Athlete: "Rory McIlroy", Img: "url" }, { Athlete: "Mike Trout", Img: "url" }, { Athlete: "Danica Patrick", Img: "url" }, { Athlete: "Drew Brees", Img: "url" }, { Athlete: "Carmelo Anthony", Img: "url" }, { Athlete: "Ryan Lochte", Img: "url" }, { Athlete: "Eli Manning", Img: "url" }, { Athlete: "Chris Paul", Img: "url" }],
    usedAthletes = ["Peyton Manning", "Tony Hawk", "Tom Brady"],
    result = gameData.filter(o => !usedAthletes.includes(o.Athlete));
      
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

const gameData = [
        {Athlete: "Peyton Manning", Img: "url"},
        {Athlete: "Tony Hawk", Img: "url"},
        {Athlete: "Tom Brady", Img: "url"},
        {Athlete: "Usain Bolt", Img: "url"},
        {Athlete: "Kevin Durant", Img: "url"},
        {Athlete: "Cristiano Ronaldo", Img: "url"},
        {Athlete: "Michael Phelps", Img: "url"},
        {Athlete: "Conor McGregor", Img: "url"},
        {Athlete: "Phil Mickelson", Img: "url"},
        {Athlete: "Stephen Curry", Img: "url"},
        {Athlete: "Rory McIlroy", Img: "url"},
        {Athlete: "Mike Trout", Img: "url"},
        {Athlete: "Danica Patrick", Img: "url"},
        {Athlete: "Drew Brees", Img: "url"},
        {Athlete: "Carmelo Anthony", Img: "url"},
        {Athlete: "Ryan Lochte", Img: "url"},
        {Athlete: "Eli Manning", Img: "url"},
        {Athlete: "Chris Paul", Img: "url"}
];

const usedAthletes = ["Peyton Manning", "Tony Hawk", "Tom Brady"];


var value = gameData.map((data) => {if(!usedAthletes.includes(data.Athlete)){return data;}}).filter((data) => data);

console.log(value);

Comments

1

Try this:

const gameData = [
        {Athlete: "Peyton Manning", Img: "url"},
        {Athlete: "Tony Hawk", Img: "url"},
        {Athlete: "Tom Brady", Img: "url"},
        {Athlete: "Usain Bolt", Img: "url"},
        {Athlete: "Kevin Durant", Img: "url"},
        {Athlete: "Cristiano Ronaldo", Img: "url"},
        {Athlete: "Michael Phelps", Img: "url"},
        {Athlete: "Conor McGregor", Img: "url"},
        {Athlete: "Phil Mickelson", Img: "url"},
        {Athlete: "Stephen Curry", Img: "url"},
        {Athlete: "Rory McIlroy", Img: "url"},
        {Athlete: "Mike Trout", Img: "url"},
        {Athlete: "Danica Patrick", Img: "url"},
        {Athlete: "Drew Brees", Img: "url"},
        {Athlete: "Carmelo Anthony", Img: "url"},
        {Athlete: "Ryan Lochte", Img: "url"},
        {Athlete: "Eli Manning", Img: "url"},
        {Athlete: "Chris Paul", Img: "url"}
]

const usedAthletes = ["Peyton Manning", "Tony Hawk", "Tom Brady"];
const newAnswerlist = [];

gameData.forEach( data => {
  if (usedAthletes.indexOf(data.Athlete) == -1) {
    newAnswerlist.push(data.Athlete);
  }
});

newAnswerlist should contain the array you want.

2 Comments

This works perfectly, I've been beating my head over this and had something a few tries ago looking similar to this but I must have messed something up. Thank you very much! :)
Awesome, glad I could help.
1

Lodash's _.differenceWith() creates an array of array values not included in the other given arrays using a comparator:

const gameData = [{"Athlete":"Peyton Manning","Img":"url"},{"Athlete":"Tony Hawk","Img":"url"},{"Athlete":"Tom Brady","Img":"url"},{"Athlete":"Usain Bolt","Img":"url"},{"Athlete":"Kevin Durant","Img":"url"},{"Athlete":"Cristiano Ronaldo","Img":"url"},{"Athlete":"Michael Phelps","Img":"url"},{"Athlete":"Conor McGregor","Img":"url"},{"Athlete":"Phil Mickelson","Img":"url"},{"Athlete":"Stephen Curry","Img":"url"},{"Athlete":"Rory McIlroy","Img":"url"},{"Athlete":"Mike Trout","Img":"url"},{"Athlete":"Danica Patrick","Img":"url"},{"Athlete":"Drew Brees","Img":"url"},{"Athlete":"Carmelo Anthony","Img":"url"},{"Athlete":"Ryan Lochte","Img":"url"},{"Athlete":"Eli Manning","Img":"url"},{"Athlete":"Chris Paul","Img":"url"}];

const usedAthletes = ["Peyton Manning", "Tony Hawk", "Tom Brady"];

const result = _.differenceWith(gameData, usedAthletes, ({ Athlete }, othVal) => Athlete === othVal);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

If you just need the athletes names, there is a simpler solution using _.difference():

const gameData = [{"Athlete":"Peyton Manning","Img":"url"},{"Athlete":"Tony Hawk","Img":"url"},{"Athlete":"Tom Brady","Img":"url"},{"Athlete":"Usain Bolt","Img":"url"},{"Athlete":"Kevin Durant","Img":"url"},{"Athlete":"Cristiano Ronaldo","Img":"url"},{"Athlete":"Michael Phelps","Img":"url"},{"Athlete":"Conor McGregor","Img":"url"},{"Athlete":"Phil Mickelson","Img":"url"},{"Athlete":"Stephen Curry","Img":"url"},{"Athlete":"Rory McIlroy","Img":"url"},{"Athlete":"Mike Trout","Img":"url"},{"Athlete":"Danica Patrick","Img":"url"},{"Athlete":"Drew Brees","Img":"url"},{"Athlete":"Carmelo Anthony","Img":"url"},{"Athlete":"Ryan Lochte","Img":"url"},{"Athlete":"Eli Manning","Img":"url"},{"Athlete":"Chris Paul","Img":"url"}];

const usedAthletes = ["Peyton Manning", "Tony Hawk", "Tom Brady"];

const result = _(gameData)
  .map('Athlete')
  .difference(usedAthletes)
  .value();

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

5 Comments

This works too. I then mapped result to just an array of athletes. thanks!
While you were jumping around accepting answers :) I've added a simpler solution for get an array of names.
Thanks. I'm just making sure to look at all answers and make sure I understand why each one works and what they're doing. So I learn how to use things like filter or the lodash difference better!
That's exactly what you should do - try and learn. It was just funny seeing the accept mark jumping around.
haha yeah, I didn't realize I couldn't say more than one answer worked. It's not fair, everyone worked!

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.