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');
I'm not sure why you are using Object.keys, but it isn't needed here
From MDN:
The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would.
So the solution becomes:
const data = statusList.filter(service => service.pm2_env.status === 'online');