-3

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: "",
  },
];
lejlun
  • 4,140
  • 2
  • 15
  • 31
gowdham
  • 27
  • 2
  • 2
    What have you tried so far? Stackoverflow is not a free coding service. You are expected to invest your best effort to solve the problem by yourself first. Then, if you fail, search the web for why it might fail. If you've done all that and still didn't succeed, come back here, show your best attempt, explain how it fails and what you expect instead. – connexo Sep 26 '21 at 07:19
  • wait i ll share my code.. – gowdham Sep 26 '21 at 07:22
  • Voting to close as a duplicate of https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects – connexo Sep 26 '21 at 07:34
  • @gowdham looks like you haven't shared your code yet. Please do share your code – I_love_vegetables Oct 04 '21 at 07:48

2 Answers2

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);
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
-1

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