ES6 Array filter() 方法没有按预期工作
ES6 Array filter() method not working as expected
我有一组对象(菜肴)需要根据它们的类别进行排序。
我有一组预定义的类别,分为三个主要组。如果一道菜属于第 1 组,它应该出现在数组的开头。如果它属于第 2 组,它应该出现在中间,如果它属于第 3 组,它应该出现在最后。 [查看代码].
我决定将原始数组创建三个单独的数组,每个数组包含过滤后的结果(根据一组),然后再次合并它们。
这是我的代码(categorySort 是这里的主要功能):
const CAT1 = ["main dish", "combo", "meal", "pasta", "pizza"];
const CAT2 = ["sandwiches", "burgers"];
const CAT3 = ['appetizers','salad','soup','frozen','healthy','catering', 'undefined'];
function belongToCategory(dishCategory, categoryArray){
categoryArray.forEach(cat => {
if(cat.localeCompare(dishCategory.toLowerCase()) === 0)
console.log(`DISH ${dishCategory.toLowerCase()} BELONGS TO A CERTAIN CATEGORY ${cat}`) //dishes are being checked properly
return true
})
return false
}
export const categorySort = (dishArray) => {
let cat1Array = dishArray.filter(dish => belongToCategory(dish.category, CAT1));
let cat2Array = dishArray.filter(dish => belongToCategory(dish.category, CAT2));
let cat3Array = dishArray.filter(dish => belongToCategory(dish.category, CAT3));
//debuggining:
console.log('array1');
console.log(cat1Array);
console.log('array2');
console.log(cat2Array);
console.log('array3');
console.log(cat3Array);
//all of the above arrays are empty
return [...cat1Array, ...cat2Array, ...cat3Array];
}
//example of dishArray:
dishArray = [
{name: 'Hummus', category: 'Appetizers'},
{name: 'Mansaf', category: 'Main Dish'},
{name: 'Cheese Burger', category: 'Burgers'},
{name: 'Fattoush', category: 'Salad'},
{name: 'Pepperoni Pizza', category: 'Pizza'},
{name: 'Shawarma', category: 'Sandwiches'},
]
但是,我想我遗漏了有关 filter 方法工作原理的一些信息,因为即使我正确地检查了一道菜是否属于某个类别,filter 方法返回的是一个空数组。
我可以让这项工作有所不同,但如果有人告诉我为什么这段代码不起作用,我将不胜感激。谢谢。
forEach
returns 中的 return true
来自 forEach
的回调,而不是 belongToCategory
函数。使用 for...of
循环代替:
function belongToCategory(dishCategory, categoryArray){
for (const cat of categoryArray) {
if(cat.localeCompare(dishCategory.toLowerCase()) === 0) {
console.log(`DISH ${dishCategory.toLowerCase()} BELONGS TO A CERTAIN CATEGORY ${cat}`) //dishes are being checked properly
return true
}
}
return false
}
还应该注意的是,这可以通过 .some
:
简单地完成
function belongToCategory(dishCategory, categoryArray){
return categoryArray.some(cat => cat === dishCategory.toLowerCase());
}
或者,由于您要检查相等性,甚至 .includes
:
function belongToCategory(dishCategory, categoryArray){
return categoryArray.includes(dishCategory.toLowerCase());
}
我有一组对象(菜肴)需要根据它们的类别进行排序。
我有一组预定义的类别,分为三个主要组。如果一道菜属于第 1 组,它应该出现在数组的开头。如果它属于第 2 组,它应该出现在中间,如果它属于第 3 组,它应该出现在最后。 [查看代码].
我决定将原始数组创建三个单独的数组,每个数组包含过滤后的结果(根据一组),然后再次合并它们。
这是我的代码(categorySort 是这里的主要功能):
const CAT1 = ["main dish", "combo", "meal", "pasta", "pizza"];
const CAT2 = ["sandwiches", "burgers"];
const CAT3 = ['appetizers','salad','soup','frozen','healthy','catering', 'undefined'];
function belongToCategory(dishCategory, categoryArray){
categoryArray.forEach(cat => {
if(cat.localeCompare(dishCategory.toLowerCase()) === 0)
console.log(`DISH ${dishCategory.toLowerCase()} BELONGS TO A CERTAIN CATEGORY ${cat}`) //dishes are being checked properly
return true
})
return false
}
export const categorySort = (dishArray) => {
let cat1Array = dishArray.filter(dish => belongToCategory(dish.category, CAT1));
let cat2Array = dishArray.filter(dish => belongToCategory(dish.category, CAT2));
let cat3Array = dishArray.filter(dish => belongToCategory(dish.category, CAT3));
//debuggining:
console.log('array1');
console.log(cat1Array);
console.log('array2');
console.log(cat2Array);
console.log('array3');
console.log(cat3Array);
//all of the above arrays are empty
return [...cat1Array, ...cat2Array, ...cat3Array];
}
//example of dishArray:
dishArray = [
{name: 'Hummus', category: 'Appetizers'},
{name: 'Mansaf', category: 'Main Dish'},
{name: 'Cheese Burger', category: 'Burgers'},
{name: 'Fattoush', category: 'Salad'},
{name: 'Pepperoni Pizza', category: 'Pizza'},
{name: 'Shawarma', category: 'Sandwiches'},
]
但是,我想我遗漏了有关 filter 方法工作原理的一些信息,因为即使我正确地检查了一道菜是否属于某个类别,filter 方法返回的是一个空数组。
我可以让这项工作有所不同,但如果有人告诉我为什么这段代码不起作用,我将不胜感激。谢谢。
forEach
returns 中的 return true
来自 forEach
的回调,而不是 belongToCategory
函数。使用 for...of
循环代替:
function belongToCategory(dishCategory, categoryArray){
for (const cat of categoryArray) {
if(cat.localeCompare(dishCategory.toLowerCase()) === 0) {
console.log(`DISH ${dishCategory.toLowerCase()} BELONGS TO A CERTAIN CATEGORY ${cat}`) //dishes are being checked properly
return true
}
}
return false
}
还应该注意的是,这可以通过 .some
:
function belongToCategory(dishCategory, categoryArray){
return categoryArray.some(cat => cat === dishCategory.toLowerCase());
}
或者,由于您要检查相等性,甚至 .includes
:
function belongToCategory(dishCategory, categoryArray){
return categoryArray.includes(dishCategory.toLowerCase());
}