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

79
Views
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 answers
Answer question

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 Report

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 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!