我们可以将两个数组使用for循环进行比较,如果两个数组中有相同的元素,就在新数组中添加这个元素,并在原数组中将这个元素删除,以保证新数组中的元素不重复。
代码示例:
function matchArray(arr1, arr2){
let newArray = [];
for(let i=0; i let arr1 = [1, 2, 2, 3, 4, 5];
let arr2 = [2, 2, 4, 6, 8];
let matchedArray = matchArray(arr1, arr2);
console.log(matchedArray); //[2, 2, 4]