在子块中获取 key:value 对

get key:value pair inside sub-blocks

我有以下来自 Postman 的输出或到达终点(我们可以说)。

{
  "SearchResult": {
    "total": 11,
    "resources": [
      {
        "id": "12345",
        "name": "GuestType",
        "description": "Identity group ",
      },
      {
        "id": "56789",
        "name": "Admin",
        "description": "",
      },
    ]
  }
}

我想从这些值中提取 "id""name"。我看到这些值在子块内。 如何使用 java-script 提取这些需要放在邮递员 "Tests" 选项卡中的键值?

尝试

let ids = data.SearchResult.resources.map(obj => obj.id);
let names = data.SearchResult.resources.map(obj => obj.name);

let data={ "SearchResult": { "total": 11, "resources": [ { "id": "12345", "name": "GuestType", "description": "Identity group ", }, { "id": "56789", "name": "Admin", "description": "", }, ] } }

let ids = data.SearchResult.resources.map(obj => obj.id);
let names = data.SearchResult.resources.map(obj => obj.name);

console.log(ids);
console.log(names);

var obj={
  "SearchResult": {
    "total": 11,
    "resources": [
      {
        "id": "12345",
        "name": "GuestType",
        "description": "Identity group ",
      },
      {
        "id": "56789",
        "name": "Admin",
        "description": "",
      },
    ]
  }
}


obj.SearchResult.resources.forEach((o)=>console.log(o.id,o.name));

下面的代码将 return 仅具有 ID 和名称的对象数组....编码愉快:)

let data = {
  "SearchResult":
  {
    "total": 11,
    "resources": [
      { "id": "12345", "name": "GuestType", "description": "Identity group ", },
      { "id": "56789", "name": "Admin", "description": "", }
    ]
  }
}

let ids = data.SearchResult.resources.map(obj => {
  id: obj.id,
  name: obj.name
});