0

This is my response body. I want to select only Collaborator's "supportNotifications" key-value set to true, how can I do that?

{
  "status": true,
  "date": "2022-04-06T16:33:13.630Z",
  "data": {
    "collaborators": [
      {
        "name": "Paul",
        "lastCheckin": "2022-03-31T19:54:50.584Z",
        "sales": 1,
        "createdAt": "2022-03-30T14:47:48.478Z",
        "id": "62446d4ab7f4da001e8f40dc",
        "supportNotification": true,
        "collaboratorId": 1
      },
      {
        "name": "John",
        "lastCheckin": null,
        "sales": 0,
        "createdAt": "2022-03-01",
        "id": "62446ea4b7f4da001e8f40df",
        "supportNotification": false,
        "collaboratorId": 6
      }
    ],
    "count": 2
  }
}

2 Answers 2

1
let response = {
  "status": true,
  "date": "2022-04-06T16:33:13.630Z",
  "data": {
    "collaborators": [
      {
        "name": "Paul",
        "lastCheckin": "2022-03-31T19:54:50.584Z",
        "sales": 1,
        "createdAt": "2022-03-30T14:47:48.478Z",
        "id": "62446d4ab7f4da001e8f40dc",
        "supportNotification": true,
        "collaboratorId": 1
      },
      {
        "name": "John",
        "lastCheckin": null,
        "sales": 0,
        "createdAt": "2022-03-01",
        "id": "62446ea4b7f4da001e8f40df",
        "supportNotification": false,
        "collaboratorId": 6
      }
    ],
    "count": 2
  }
};

const collaborators = response.data.collaborators.filter(c => c.supportNotification);
Sign up to request clarification or add additional context in comments.

Comments

1

This will give you each collaborator object that has supportNotification = true. Is this the result you are trying to select?

var body = {
  status: true,
  date: "2022-04-06T16:33:13.630Z",
  data: {
    collaborators: [
      {
        name: "Paul",
        lastCheckin: "2022-03-31T19:54:50.584Z",
        sales: 1,
        createdAt: "2022-03-30T14:47:48.478Z",
        id: "62446d4ab7f4da001e8f40dc",
        supportNotification: true,
        collaboratorId: 1,
      },
      {
        name: "John",
        lastCheckin: null,
        sales: 0,
        createdAt: "2022-03-01",
        id: "62446ea4b7f4da001e8f40df",
        supportNotification: false,
        collaboratorId: 6,
      },
    ],
    count: 2,
  },
};

var result = [];

body.data.collaborators.forEach((item) => {
  if (item.supportNotification === true) {
    result.push(item);
  }
});

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.