Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

83
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda