天天看点

根据一个数组过滤另一个数组

<!DOCTYPE html>
<html >

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        //筛选数组:[{id:2},{id:3}]
        let ids = [2, 3];
        let arr = [{
                id: 1
            },
            {
                id: 2
            },
            {
                id: 3
            }
        ];
        let newArr = [];

        arr.forEach(item => {
            if (ids.includes(item.id)) {
                newArr.push(item);
            };
        })

        console.log(newArr); //输出:[{id:2},{id:3}]
    </script>
</body>
</html>