gpt4 book ai didi

javascript - 过滤 js 中长度小于 < 50 个字符的内容

转载 作者:行者123 更新时间:2023-12-02 16:57:44 26 4
gpt4 key购买 nike

我正在研究简单的js函数...GetShortMessages 接受具有“.message”属性的对象数组,并返回长度小于 50 个字符的消息数组。在下面的 fiddle 中提供我的代码..基本上我想学习一个过滤器:

function filteredMessages(messages){
return messages.length < 50;
}

function getShortMessages(messages) {
// SOLUTION GOES HERE
var filtered = messages.filter(filteredMessages);
return filtered;
}

module.exports = getShortMessages

当我验证时收到以下错误

验证“基本:过滤器”:

ACTUAL                             EXPECTED
------ --------
"[]" != "[ 'Ea occaecat adipisicing anim voluptate mollit.', "
"" != " 'Incididunt mollit dolore commodo ipsum.', "
null != " 'Veniam irure non Lorem enim nostrud.', "
null != " 'Excepteur commodo nulla consequat nostrud mollit.', "
null != " 'Eiusmod enim sunt aliqua veniam mollit.', "
null != " 'Sunt nisi deserunt labore ut ipsum nostrud.', "
null != " 'Ad officia proident irure sit.', "
null != " 'Ex laborum eu elit irure velit anim commodo.', "
null != " 'Eu laboris adipisicing non ea.', "
null != " 'Adipisicing cillum officia et nostrud.', "
null != " 'Pariatur laboris officia eiusmod Lorem.', "
null != " 'Aliqua esse reprehenderit proident deserunt.', "
null != " 'Nulla commodo enim sint excepteur duis ea.', "
null != " 'Aliqua elit culpa consequat consectetur.', "
null != " 'Mollit dolore nostrud officia dolore.', "
null != " 'Amet laboris labore labore minim id.', "
null != " 'Mollit aute labore dolor nisi irure dolor.', "
null != " 'Laborum velit mollit anim cupidatat ad mollit.', "
null != " 'Sit do pariatur pariatur velit ipsum.', "
null != " 'Adipisicing proident minim minim minim.', "
null != " 'Labore quis sit ut sit id.', "
null != " 'Labore eu nisi deserunt ea occaecat consequat.', "
null != " 'Exercitation culpa nostrud incididunt aliquip.', "
null != " 'Eu sunt amet do ea sunt adipisicing nostrud.', "
null != " 'Incididunt magna id qui cillum do.' ]"
null != ""
# FAIL

最佳答案

您的 filteredMessages 函数需要接受一个参数,因为 Array.filter 方法将回调应用于每个元素,将该元素作为参数传递给所提供的回调。

function filteredMessages(message){
return message.length < 50;
}

您实际上不必为此编写任何单独的函数,而是可以使用匿名函数:

messages.filter(function(message){
return message.length < 50;
});

关于javascript - 过滤 js 中长度小于 < 50 个字符的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26048178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com