【问题标题】:first object key value compare with the another object key?第一个对象键值与另一个对象键比较?
【发布时间】:2021-05-30 20:39:23
【问题描述】:

我有两个数据,一个是对象数组,第二个是映射对象。所以我试图实现这样的逻辑,我的第一个对象及其数组的 subarr 对象具有键值id:1 然后匹配 1 可用作花药映射对象中的键。那我该如何解决呢?

数据1(对象数组)

[
  {
    "id": 1,
    "title": "Dashboard",
    "route": "Dashboard",
    "routeid": 1,
    "key": "dashboard",
    "icon": "home",
    "subarr": []
  },
  {
    "id": 2,
    "title": "Pages",
    "route": "Pages",
    "routeid": 2,
    "key": "pages",
    "icon": "diamond",
    "subarr": [
      {
        "id": 3,
        "title": "Tabs",
        "route": "Tabs",
        "key": "tab",
        "routeid": 3,
        "icon": "grid"
      },
      {
        "id": 4,
        "route": "Test",
        "key": "test",
        "icon": "adn",
        "routeid": 4,
        "title": "Test"
      }
    ]
  },
  {
    "id": 5,
    "title": "Components",
    "key": "Components",
    "route": "component",
    "routeid": 5,
    "icon": "notebook",
    "subarr": [
      {
        "id": 6,
        "route": "Card",
        "key": "card",
        "icon": "i-card",
        "routeid": 6,
        "title": "Card"
      },
      {
        "id": 7,
        "title": "Button",
        "route": "Button",
        "routeid": 7,
        "key": "button",
        "icon": "control-play"
      },
      {
        "id": 8,
        "title": "Table",
        "route": "Table",
        "key": "table",
        "routeid": 8,
        "icon": "list"
      },
      {
        "id": 9,
        "title": "Charts",
        "route": "Chart",
        "routeid": 9,
        "key": "chart",
        "icon": "chart"
      }
    ]
  },
  {
    "id": 10,
    "title": "notifications",
    "route": "User",
    "key": "user",
    "routeid": 10,
    "icon": "bell",
    "subarr": []
  },
  {
    "id": 11,
    "title": "User profile",
    "route": "Profile",
    "routeid": 11,
    "key": "profile",
    "icon": "user",
    "subarr": []
  },
  {
    "id": 12,
    "title": "Carousel",
    "route": "Carousel",
    "key": "carousel",
    "routeid": 12,
    "icon": "layers",
    "subarr": []
  }
]

映射对象:

{
  "1": "dashboard",
  "2": "pages",
  "3": "tab",
  "4": "test",
  "6": "card",
  "7": "button",
  "8": "table",
  "9": "chart",
  "10": "user",
  "11": "profile",
  "12": "carousel"
}

【问题讨论】:

  • 您的最终结果是什么?只是检查1的键值是否存在于映射对象中?
  • @alisasani 感谢重播,我只想检查值 1 是否作为映射对象中的键存在?
  • 您觉得我的回答有帮助吗?

标签: javascript arrays reactjs react-native object


【解决方案1】:

sandbox

let exists = [];

data1.map((el, id) => {
      if (Object.keys(mapping).includes(el.id.toString()))
        exists = [...exists, { id: el.id, exists: true }];
      else exists = [...exists, { id: el.id, exists: false }];
      el.subarr.map((l, i) => {
        if (Object.keys(mapping).includes(l.id.toString()))
          exists = [...exists, { id: l.id, exists: true }];
          else exists = [...exists, { id: l.id, exists: false }];
      });
    });

exists 数组将包含 id 和每个 id 的状态,无论它们是否存在于映射对象中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 2018-08-04
    相关资源
    最近更新 更多