Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda