Filter NodeJs Data


app.get('/contactx', (req, res) => {
    console.log('axaac');
    const {search, limit} =req.query
    let sortedProducts = [...products]

    if(search){
        sortedProducts = sortedProducts.filter((product)=>{
            return product.name.startsWith(search)
        })
    }
    if(limit){
        sortedProducts = sortedProducts.slice(0, Number(limit))
        }
        if(sortedProducts.length<0){
            return res.status(402).json({success: true},{data:[]})
        }

        return res.status(402).json(sortedProducts)
})
Embarrassed Elephant