如何在对象数组中设置基于True和False的特定键?

时间:2018-09-25 11:07:29

标签: javascript json

我正在调用一个API并获得响应(下面是响应) 我从会话中获取了JOHN_FARY,我只想检查JOHN_FARY的键值,所有_id的Even View都是false,那么它应该返回一个false值。

假设,如果我得到JACKY_MANY,则检查应该相同。

[
  {
    "_id": "app-first",
    "project": "Backend",
    "accessForPortal": "userPortal",
    "visible": true,
    "JOHN_FARY": {
      "View": true,
      "Down": true,
      "Up": true,
      "Left": true
    },
    "JACKY_MANY": {
      "View": true,
      "Down": true,
      "Up": true,
      "Left": true
    },
    "JACK_MAN": {
      "View": true,
      "Down": true,
      "Up": true,
      "Left": true
    },
    "GROUND_POL": {
      "View": true,
      "Down": true,
      "Up": true,
      "Left": true
    },
    "_etag": {
      "$oid": "5b7c07364b402e87085d9d7f"
    }
  },
  {
    "_id": "app-rule-second",
    "project": "Backend",
    "accessForPortal": "userPortal",
    "JOHN_FARY": {
      "View": true,
      "Enable": true,
      "Edit": true,
      "Delete": true
    },
    "JACKY_MANY": {
      "View": true,
      "Enable": true,
      "Edit": true,
      "Delete": true
    },
    "JACK_MAN": {
      "View": true,
      "Enable": true,
      "Edit": true,
      "Delete": true
    },
    "GROUND_POL": {
      "View": true,
      "Enable": true,
      "Edit": true,
      "Delete": true
    },
    "_etag": {
      "$oid": "5b7c07364b402e87085d9d7f"
    }
  },
  {
    "_id": "app-thirs-rules",
    "project": "Backend",
    "visible": true,
    "accessForPortal": "userPortal",
    "JOHN_FARY": {
      "View": true,
      "Enable": true,
      "Edit": true,
      "Delete": true
    },
    "JACKY_MANY": {
      "View": true,
      "Enable": true,
      "Edit": true,
      "Delete": true
    },
    "JACK_MAN": {
      "View": true,
      "Enable": true,
      "Edit": true,
      "Delete": true
    },
    "GROUND_POL": {
      "View": true,
      "Enable": true,
      "Edit": true,
      "Delete": true
    },
    "_etag": {
      "$oid": "5b7c07364b402e87085d9d7f"
    }
  }
]

1 个答案:

答案 0 :(得分:2)

方法很多,但是这应该是最简单的。

function getView(name){
     return response.some(e=>e[name].View === true)
}

getView('JOHN_FARY') // true