我有一个gameData列表和使用过的运动员。我需要创建一个新的数组,其中只包含来自gameData的不在usedAthletes中的Athlete。我老实地看了一遍,在过去的一天左右尝试过lodash和各种各样的东西。任何建议都会很棒。如果ES6方法有效,那么知道它也很酷:)
{"studentsinfo":[{"id":"1","name":"name1"},{"id":"2","name":"name2"} , ... {"id":"n","name":"namen"}]}
答案 0 :(得分:1)
您可以将Array#filter
与Array#includes
一起使用,并排除usedAthletes
。
您的解决方案不起作用,因为您在每次迭代中初始化结果数组。
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; }
答案 1 :(得分:1)
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>
&#13;
如果您只需要运动员姓名,可以使用_.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);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
&#13;
答案 2 :(得分:1)
使用过滤器后跟map,
const newAnswerlist = gameData
.filter(data => usedAthletes.indexOf(data.Athlete) !== -1)
.map(data => data.Athlete);
如果您也想要img信息,请仅使用过滤器
const newAnswerlist = gameData
.filter(data => usedAthletes.indexOf(data.Athlete) !== -1);
答案 3 :(得分: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);
答案 4 :(得分: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"];
const newAnswerlist = [];
gameData.forEach( data => {
if (usedAthletes.indexOf(data.Athlete) == -1) {
newAnswerlist.push(data.Athlete);
}
});
newAnswerlist
应包含您想要的数组。