js两个数组取相同的值的几种方式,包含ES6

_resultSameValue(arr1, arr2) {
    let newArr = [];
    for (let i = 0; i < arr1.length; i++) {
      if (arr2.includes(arr1[i])) {
        newArr.push(arr1[i])
      }
    }
    return newArr;
  }
 _resultSameValue2(arr1, arr2) {
    let newArr = arr1.filter(t => arr2.includes(t));
    return newArr;
  }

下面这种方式会去重,因为会保证key的唯一性。
 

_resultSameValue1(arr1, arr2) {
    let newArr = Array.from(new Set([...arr1].filter(x => arr2.includes(x))));
    return newArr;
  }

 

你可能感兴趣的:(技术,js,filter,两个数组取相同值,includes)