マップ、フィルタのアレイとの間の差、のforEach

彼は今日の手を練習するとき、私たちは、これらの三つの方法の下での使用状況を分析しています。
A.地図は、
それが新しい配列を返す関数への各呼び出し後に元の配列です。

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

.filter二つの
それは戻って真または偽のフィルタ機能は、項目が、それはまた、同じ戻り、新たな配列である、新しい配列の内部に行くことができるかどうかを判断することを各コールフィルタ機能元の配列です。

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

三。foreachの
私は...のためにその章を書いた前に、コンクリートが見ることができる、それが各関数の呼び出し元の配列である,?しかし、それは継続し、返すことができないの内側に、休憩...

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

// expected output: "a"
// expected output: "b"
// expected output: "c"

公開された11元の記事 ウォンの賞賛3 ビュー8777

おすすめ

転載: blog.csdn.net/weixin_44233892/article/details/104720117