js从一个数组中筛选出另一个数组中存在的值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<p>您好</p>
<script>
let arr1 = ['a', 'b', 'c']
let arr2 = [
{ str: 'a', num: 1 },
{ str: 'as', num: 2 },
{ str: 'b', num: 3 },
{ str: 'ca', num: 4 },
{ str: 'c', num: 5 },
{ str: 'ddd', num: 6 },
]
// let arr3 = arr2.filter(checkAdult);
// function checkAdult(age){
// for(let i=0; i<arr1.length; i++){
// if(age.str == arr1[i]){
// return true
// }
// }
// }
// console.log(arr3)
let arr3 = []
arr1.forEach((item) => {
arr2.forEach((element) => {
if (item == element.str) {
arr3.push(element)
}
})
// return arr3
})
console.log(arr3)
</script>
</body>
</html>
https://blog.csdn.net/qq_35018214/article/details/103429020