forEachメソッドで書くと長くなるコードをfilterメソッドを使うと簡潔に書ける。
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = [];
words.forEach((element, i2) => {
if (element.length > 6) {
result.push(element);
}
});
console.log("result = " + result);
const result2 = words.filter(word => word.length > 6);
console.log("result2 = " + result2);
/*
result = exuberant,destruction,present
result2 = exuberant,destruction,present
*/
Array.prototype.filter() – JavaScript | MDN (mozilla.org) ExternalLink