Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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');

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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');

about 4 years ago · Juan Pablo Isaza Report

0

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');
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!