因此,我正在尝试学习高阶函数,并且正在做练习等。我需要将结果返回长度小于6个字符的原始数组输入的元素。但是我认为我搞砸了语法。不知道我该如何使用高阶函数来做到这一点。帮助吗?
var courses = [calculus,cs17,litr0100,engn];
reduce(function() {
courses.filter(courses => courses.length > 6);
return courses;
})
答案 0 :(得分:1)
我在做什么错了?
var courses = [calculus,cs17,litr0100,engn];
// ^ unless these are variables this will fail
// use let or const
reduce(function() {
// javascript does not know a reduce function
// the reducer (lambda) function has no parameters
courses.filter(courses => courses.length > 6);
// ^ should be 'course' or something else then 'courses'
// filtering with the right syntax would've been sufficient
return courses;
})
如此
const courses = "calculus,cs17,litr0100,engn".split(",");
const coursesFiltered = courses.filter(course => course.length > 6);
// ^ assign the filtered array to a different array
// Array.filter doesn't mutate (change) the original
console.log(courses);
console.log(coursesFiltered);