Javascript 按特定属性筛选和设置数组
您好,我有以下JavaScript对象数组:Javascript 按特定属性筛选和设置数组,javascript,filter,Javascript,Filter,您好,我有以下JavaScript对象数组: runners = [ { id: 1, first_name: "Charmain", last_name: "Seiler", email: "cseiler0@wired.com", shirt_size: "2XL", company_name: "Divanoodle", donation: 75 }, { id: 2, first_name: "Whitaker", last_name: "Ierland",
runners = [
{ id: 1, first_name: "Charmain", last_name: "Seiler", email: "cseiler0@wired.com", shirt_size: "2XL", company_name: "Divanoodle", donation: 75 },
{ id: 2, first_name: "Whitaker", last_name: "Ierland", email: "wierland1@angelfire.com", shirt_size: "2XL", company_name: "Wordtune", donation: 148 },
{ id: 3, first_name: "Julieta", last_name: "McCloid", email: "jmccloid2@yahoo.com", shirt_size: "S", company_name: "Riffpedia", donation: 171 },
{ id: 4, first_name: "Martynne", last_name: "Paye", email: "mpaye3@sciencedaily.com", shirt_size: "XL", company_name: "Wordware", donation: 288 },
{ id: 5, first_name: "Gussy", last_name: "Raraty", email: "graraty4@ucoz.ru", shirt_size: "L", company_name: "Oozz", donation: 291 },
];
我正在努力解决以下挑战,我真的不知道我做错了什么。
如果有人能帮忙,这就是我写的,我知道这是不正确的,但我不知道为什么
/**
* ### Challenge `getRunnersByTShirtSize`
*
* @instructions
* The event director needs a way to find the runners that need
* a specific t-shirt size, so they can place the orders easily.
* Implement this function using filter().
*
* @param runners array of runners like the one inside the /data/runners.js file.
* @param tShirtSize string (possible values are "S", "M", "L", "XL", "2XL", "3XL").
* @returns an array containing only the runners that use the given `tShirtSize`.
* The runners in the array appear in the same order they appear in the `runners` array.
*/
function getRunnersByTShirtSize(runners, tShirtSize) {
/* CODE HERE */
const newSize = runners.filter((size) => {
return runners.size == tShirtSize;
});
return newSize;
}
它应该是runner.shirt\u尺码,而不是runner.size
您应该引用在函数filterrunner中传递的项
与T恤尺寸相关的对象中的键是什么大小不是shirt\u size。@t与tShirtSize相关的键是shirt\u size。为什么要将tShirtSize与数组大小进行比较?所以这是一个输入错误。请检查我的更新答案,您不应该引用过滤器中的跑步者
function getRunnersByTShirtSize(runners, tShirtSize) {
/* CODE HERE */
const newSize = runners.filter((runner) => {
return runner.shirt_size == tShirtSize; //<===== not runners.size but instead shirt_size
});
return newSize;
}
function getRunnersByTShirtSize(runners, tShirtSize) {
return runners.filter(runners=> runners.shirt_size == tShirtSize);
}