简体   繁体   English

通过将对象数组与数组进行比较来创建新数组

[英]Creating a new array by comparing an array of objects to an array

I have a list of gameData and used athletes. 我有一份gameData和二手运动员的清单。 I need to make a new array that only contains Athlete from gameData that is not in usedAthletes. 我需要制作一个仅包含来自gameData中不在Ausletes中的运动员的新数组。 I've honestly looked all over, tried lodash and various things over the past day or so. 老实说,我过去一整天都在看,试过lodash和其他各种东西。 Any advise would be great. 任何建议将是巨大的。 If there are ES6 methods that work it would be cool to know that too :) 如果有可以使用的ES6方法,也很高兴知道这一点:)

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

You could use Array#filter with Array#includes and exclude the usedAthletes . 您可以将Array#filterArray#includes并排除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; } 

Lodash's _.differenceWith() creates an array of array values not included in the other given arrays using a comparator: Lodash的_.differenceWith()使用比较器创建了一个数组值数组,该数组值数组未包含在其他给定数组中:

 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() : 如果您只需要运动员的名字,可以使用_.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> 

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, 如果您也想要img信息,请仅使用过滤器,

const newAnswerlist = gameData
.filter(data => usedAthletes.indexOf(data.Athlete) !== -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); 

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. newAnswerlist应该包含所需的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM