在JavaScript中按字符串长度过滤数组 [英] Filter array by string length in javascript
本文介绍了在JavaScript中按字符串长度过滤数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我真的是javascript的初学者
I'm a really beginner in javascript
我有一个包含的数组
array_1 = ["the","quick","brown","fox","jumped","over","the","lazy","dog"]
我想要另一个数组,但只包含长度> 3的单词
And I want another array but only with words that have length > 3
array_cleaned = ["quick","brown","jumped","lazy"]
当前实施方式:
array_1 = ["the","quick","brown","fox","jumped","over","the","lazy","dog"]
array_clean = [];
for(var i=0; i<array_1.length; i++){
if(array_1[i].length > 3)
array_clean.push(array_1[i]);
}
document.write(array_clean)
但是我正在寻找一种实际过滤它的方法.有什么想法吗?
But I'm looking for a way to actually filter it. Any ideas?
推荐答案
You can use filter
method:
var array_1 = ["the","quick","brown","fox","jumped","over","the","lazy","dog"];
var array_cleaned = array_1.filter( function( element ) {
return element.length > 3;
});
console.log(array_cleaned); //["quick", "brown", "jumped", "over", "lazy"]
这篇关于在JavaScript中按字符串长度过滤数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文