Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JScript:将字符串数组作为输入,返回只包含少于五个字符的字符串的新数组_Javascript - Fatal编程技术网

Javascript JScript:将字符串数组作为输入,返回只包含少于五个字符的字符串的新数组

Javascript JScript:将字符串数组作为输入,返回只包含少于五个字符的字符串的新数组,javascript,Javascript,如果狮子和麋鹿少于五个字符,我如何才能使其只返回lion和elk 谢谢 您可以使用.filter()将数组向下筛选为一个新数组,该数组包含.length小于6个字符的字符串 function fiveChar(inputArray) { var output = input.join(); return output; } console.log(fiveChar(['lion', 'gorilla', 'elk', 'kangaroo'])); 函数fiveChar(输入阵

如果狮子和麋鹿少于五个字符,我如何才能使其只返回
lion
elk

谢谢

您可以使用
.filter()
将数组向下筛选为一个新数组,该数组包含
.length
小于6个字符的字符串

function fiveChar(inputArray) {
    var output = input.join();
    return output;
}

console.log(fiveChar(['lion', 'gorilla', 'elk', 'kangaroo']));
函数fiveChar(输入阵列){
返回inputArray.filter(函数(in){
返回长度<6;
});
}

返回inputArray.filter(x=>x.length
input.filter(s=>s.length<5.join)()
有没有一种使用循环的方法?@raisesHand
filter
是一种循环机制。但是可以使用
for
手动实现。创建虚拟数组并将必要的元素推入其中并返回it@raisesHand:是的。您编写一个循环,并在其中进行比较,为要k的数组推送到一个新数组但是如果这个警告很重要的话,它就应该在问题中。我们不需要再回答“如何过滤数组?”
function fiveChar(inputArray) {
  return inputArray.filter(function(in) { 
    return in.length < 6;
  });
}