実装したいこと
単語で構成された2つの配列から重複する単語を抜き出したいです。
javascript
1 let arr1 = ['hello', 'world'] 2 let arr2 = ['hello', 'earth'] 3 let result = [] 4 arr1.forEach(el => { 5 arr2.forEach(el2 => { 6 if (el === el2) result.push(el2) 7 }) 8 }) 9 console.log(result); // ['hello']
これは動きます、arr2のとこをfilterで実装すると
javascript
1 let arr1 = ['hello', 'world'] 2 let arr2 = ['hello', 'earth'] 3 let result = [] 4 arr1.forEach(el => { 5 result = arr2.filter(el2 => el2 === el) 6 }) 7 console.log(result) //[]
これは動きません。
このfilterの使い方のどこが間違っているのでしょうか。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/14 01:09