循环通过对象JavaScript

发布于 2025-01-19 12:35:25 字数 721 浏览 1 评论 0原文

这是我的回复正文。我只想选择 Collaborator 的“supportNotifications”键值设置为 true,我该怎么做?

{
  "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
  }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

渔村楼浪 2025-01-26 12:35:25
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);
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);
埋葬我深情 2025-01-26 12:35:25

这将为您提供每个具有 supportNotification = true 的协作者对象。这是您想要选择的结果吗?

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文