Returns an array of elements that appear in both arrays.
Use Array.prototype.filter() to remove values that are not part of values, determined using Array.prototype.includes().
比較兩組陣列,回傳他們共同的陣列
1.使用 Array.prototype.filter() 過濾出不是相同的值,採取使用
2.filter()裡面條件設定就由Array.prototype.includes()來決定
const similarity = (arr, values) => arr.filter(v => values.includes(v));
//EXAMPLES
similarity([1, 2, 3], [1, 2, 4]); // [1, 2]
const items = [12, 1, 7, 5, 4, 2, 9];
const lessThanFive = items.filter(element=> element < 5);
console.log(lessThanFive)
根據函數回傳值 ( True / False)決定要不要把元素複製到新陣列
includes() 方法會判斷陣列是否包含特定的元素,並以此來回傳 true 或 false。
let array = ['JS', JAVA', 'ECMA'];
let searchElement = 'ECMA';
console.log(array.includes(searchElement)); // true
MDN Array.prototype.includes()