Javascript 如何从不包括最长字符串的数组中筛选字符串?

Javascript 如何从不包括最长字符串的数组中筛选字符串?,javascript,arrays,Javascript,Arrays,我正在尝试获取不包括最长字符串的数组。我的代码返回一个空数组 我的代码: 康斯特学生=[ “安迪”、“托马斯”、“伊丽莎白”、“维琪” ] 学生们 const longestName=students.reducenameOfMaxLen console.loglongestName 函数名MaxLenName1,name2{ 返回name1.length>name2.length?name1:name2; } const allButTheLongest=students.filterfun

我正在尝试获取不包括最长字符串的数组。我的代码返回一个空数组

我的代码:

康斯特学生=[ “安迪”、“托马斯”、“伊丽莎白”、“维琪” ] 学生们 const longestName=students.reducenameOfMaxLen console.loglongestName 函数名MaxLenName1,name2{ 返回name1.length>name2.length?name1:name2; } const allButTheLongest=students.filterfunctionname{ 名字!=最长的名字; } logallButTheLongest您只是忘记了从过滤器中的回调函数返回

您需要返回true或false,以便filter命令知道是否在过滤输出中包含该值。您进行了计算,但没有返回值。如果您没有返回任何内容,它将假定为false,即过滤掉它

康斯特学生=[ “安迪”、“托马斯”、“伊丽莎白”、“维琪” ] 学生们 const longestName=students.reducenameOfMaxLen console.loglongestName 函数名MaxLenName1,name2{ 返回name1.length>name2.length?name1:name2; } const allButTheLongest=students.filterfunctionname{ return name!=longestName;//不要伪造return关键字! } logallButTheLongest您只是忘记了从过滤器中的回调函数返回

您需要返回true或false,以便filter命令知道是否在过滤输出中包含该值。您进行了计算,但没有返回值。如果您没有返回任何内容,它将假定为false,即过滤掉它

康斯特学生=[ “安迪”、“托马斯”、“伊丽莎白”、“维琪” ] 学生们 const longestName=students.reducenameOfMaxLen console.loglongestName 函数名MaxLenName1,name2{ 返回name1.length>name2.length?name1:name2; } const allButTheLongest=students.filterfunctionname{ return name!=longestName;//不要伪造return关键字! }
console.logallButTheLongest过滤器的回调不会返回值

任用

const allButTheLongest = students.filter(name => name != longestName)


筛选器的回调不返回值

任用

const allButTheLongest = students.filter(name => name != longestName)

您忘记在筛选器处理程序函数的比较前面加上一个返回

康斯特学生=['Andy'、'Thomas'、'Elizabeth'、'Vicky']; const longestName=students.reducenameOfMaxLen console.loglongestName 函数名MaxLenName1,name2{ 返回name1.length>name2.length?name1:name2; } const allButTheLongest=students.filterfunctionname{ 返回名称!=最长名称; } logallButTheLongest您忘记在过滤器处理程序函数的比较前面加上一个返回

康斯特学生=['Andy'、'Thomas'、'Elizabeth'、'Vicky']; const longestName=students.reducenameOfMaxLen console.loglongestName 函数名MaxLenName1,name2{ 返回name1.length>name2.length?name1:name2; } const allButTheLongest=students.filterfunctionname{ 返回名称!=最长名称; } console.logallbutthelongestdodesconsole.loglongestName是否在控制台中记录预期值?console.loglongestName是否在控制台中记录预期值?