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

81
Vistas
Filter array of nested object using javascript

I want to filter specific object using nested object element this

"token": "4f1f17f6503e4c5a3a269ecf93d6c92d"

This my data:

const data = [
  {
    name: "test",
    token: {
      expiryTime: "2021-09-24T12:27:30.654Z",
      purpose: "ForgotPassword3",
      token: "4f1f17f6503e4c5a3a269ecf93d6c92d",
    },
    user_id: "acaacc940c9ebfe798dee68acf5c",
    zipcode: "",
  },
  {
    name: "df ",
    token: null,
    user_id: "e0efe9810ca289ccd590bce48051",
    zipcode: "",
  },
  {
    name: "Io",
    phone: "88888888",
    state: "NY",
    token: null,
    user_id: "c88ce38d0c86f786c3a4b0f9f967",
    zipcode: "13201",
  },
];

Expected output is: Data array inside the first object of token using filter object. Below given expected out.

const data = [
  {
    name: "test",
    token: {
      expiryTime: "2021-09-24T12:27:30.654Z",
      purpose: "ForgotPassword3",
      token: "4f1f17f6503e4c5a3a269ecf93d6c92d",
    },
    user_id: "acaacc940c9ebfe798dee68acf5c",
    zipcode: "",
  },
];
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you want a specific object, you could use find instead of filter. find will return the first element which verifies the condition specified to the find method where as the filter method is used to filters all the elements and returns all the element that matches the condition withing an array.

  • *You need to add the optional chaining ?. because the token object might be null like you have in some of your data here both examples:

const data = [
  {
    
    "name": "test",
    "token": {
      "expiryTime": "2021-09-24T12:27:30.654Z",
      "purpose": "ForgotPassword3",
      "token": "4f1f17f6503e4c5a3a269ecf93d6c92d"
    },
    "user_id": "acaacc940c9ebfe798dee68acf5c",
    "zipcode": ""
  },
  {
    
    "name": "df ",
    "token": null,
    "user_id": "e0efe9810ca289ccd590bce48051",
    "zipcode": ""
  },
  {
    
    "name": "Io",
    "phone": "88888888",
    "state": "NY",
    "token": null,
    "user_id": "c88ce38d0c86f786c3a4b0f9f967",
    "zipcode": "13201"
  }
]

const resFilter = data.filter(x => x.token?.token === "4f1f17f6503e4c5a3a269ecf93d6c92d");

console.log(resFilter);

const resObj = data.find(x => x.token?.token === "4f1f17f6503e4c5a3a269ecf93d6c92d");

console.log(resObj);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You must use the following code const finded = data.filter(user => user?.token?.token ==="value"}) console.log(finded);

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