javascript - filtering item that has nested objects based on condition and returning the item
问题描述
I have a status list array from an api & i want to filter out the service that has stopped, so if 'online' I want to return the item in console or state. how can I achieve this? can someone tell me what i'm doing wrong. here is my code -
const statusList = [
{
"pid": 0,
"name": "dailyScripts.job",
"pm2_env": {
"namespace": "default",
"kill_retry_time": 100,
"windowsHide": true,
"username": "u4011",
"treekill": true,
"status": "stopped"
},
"pm_id": 0,
"monit": {
"memory": 0,
"cpu": 0
}
},
{
"pid": 1,
"name": "finn.job",
"pm2_env": {
"namespace": "default",
"kill_retry_time": 100,
"windowsHide": true,
"username": "u3411",
"treekill": true,
"status": "online"
},
"pm_id": 1,
"monit": {
"memory": 0,
"cpu": 1
}
}
]
const data = statusList.filter(service => Object.keys(service.pm2_env.status) === 'online');
console.log(data, 'data');
解决方案
The Object.keys()
call is not necessary here, without it, the filter() result should be as you expect:
const statusList = [ { "pid": 0, "name": "dailyScripts.job", "pm2_env": { "namespace": "default", "kill_retry_time": 100, "windowsHide": true, "username": "u4011", "treekill": true, "status": "stopped" }, "pm_id": 0, "monit": { "memory": 0, "cpu": 0 } }, { "pid": 1, "name": "finn.job", "pm2_env": { "namespace": "default", "kill_retry_time": 100, "windowsHide": true, "username": "u3411", "treekill": true, "status": "online" }, "pm_id": 1, "monit": { "memory": 0, "cpu": 1 } } ]
const data = statusList.filter(service => service.pm2_env.status === 'online');
console.log(data, 'data');
推荐阅读
- scala - Spark&hbase: java.io.IOException: Connection reset by peer
- javascript - 更新 Javascript 中的会话变量
- php - 如何在php中上传多个图像并将其存储为blob mysql
- javascript - 使用 javascript 扩展 typescript 库
- diagram - GOJS 第二个图表的缩放问题
- django - ImproperlyConfigured:请求设置 INSTALLED_APPS,但未配置设置
- json - 如何使用卷来持久化我的 mongodata?
- c# - 在 azure protal 中连接两个应用服务和简易表格
- python - 如何整齐地打印字典?
- sql - 如果路径键在变量 postgresql 中,如何更新 jsonb 值