怎么样为一个数组筛选数据在server.js里面
在server.js中筛选数组数据可以使用Array.prototype.filter()方法。以下是一个示例,演示如何在/healthReport接口中筛选数据:
const express = require('express'); const app = express();
const healthReport = [ { serviceName: 'rest', workspace: 'default', status: true }, { serviceName: 'graphql', workspace: 'default', status: false }, { serviceName: 'rest', workspace: 'production', status: true }, { serviceName: 'grpc', workspace: 'production', status: true } ];
app.get('/healthReport', (req, res) => { const filteredReport = healthReport.filter(report => report.serviceName === 'rest'); res.json(filteredReport); });
app.listen(3000, () => { console.log('Server is running on port 3000'); });