简体   繁体   English

JavaScript:获取二维数组中某个字段的所有可能值

[英]JavaScript: get all possible values of a field in a two-dimensional array

From API I get such a json:从 API 我得到这样一个 json:

{
    "purposes": [
        {
            "id": 1,
            "code": "for-calls"
        },
        {
            "id": 2,
            "code": "task-management"
        },
        {
            "id": 3,
            "code": "messenger"
        }
    ],
    "availabilities": [
        {
            "id": 1,
            "code": "free"
        },
        {
            "id": 2,
            "code": "free-basic-plan"
        },
        {
            "id": 3,
            "code": "trial-version"
        }
    ],
    "ecosystems": [
        {
            "id": 1,
            "code": "browse-widget"
        },
        {
            "id": 2,
            "code": "web-app"
        },
        {
            "id": 3,
            "code": "installation-on-your-server"
        }
    ]
}

How do I iterate over this json to get a new array containing the field values 'code' from each element.如何遍历此 json 以获取包含每个元素的字段值“代码”的新数组。 As a result, there should be /catalog/filter/value_of_code in each element of the array.因此,数组的每个元素中都应该有/catalog/filter/value_of_code

const obj = {
  "purposes": [
    {
      "id": 1,
      "code": "for-calls"
    },
    {
      "id": 2,
      "code": "task-management"
    },
    {
      "id": 3,
      "code": "messenger"
    }
  ],
  "availabilities": [
    {
      "id": 1,
      "code": "free"
    },
    {
      "id": 2,
      "code": "free-basic-plan"
    },
    {
      "id": 3,
      "code": "trial-version"
    }
  ],
  "ecosystems": [
    {
      "id": 1,
      "code": "browse-widget"
    },
    {
      "id": 2,
      "code": "web-app"
    },
    {
      "id": 3,
      "code": "installation-on-your-server"
    }
  ]
}

const arr = Object.values(obj).reduce((acc, e) => [...acc, ...e.map(o => `/catalog/filter/${o.code}`)] ,[]);

console.log( arr );

Lets say you are receiving this JSON object in serverResp object.假设您在 serverResp object 中收到此 JSON object。 Now lets check if any catalogs are present in the object.现在让我们检查 object 中是否存在任何目录。

if(Object.keys(serverResp).length)

Next we shall iterate over the catalogs接下来我们将遍历目录

Object.keys(serverResp).forEach((cat) => )

Each cat represents a catalog.每只猫代表一个目录。 Next we shall fetch array against each key inside our forloop接下来,我们将针对 forloop 中的每个键获取数组

const catItems = serverResp[cat];

catItems will contain all the items of a particular catalog. catItems 将包含特定目录的所有项目。 Now lets iterate over each catItems to get the "code" value现在让我们遍历每个 catItems 以获得“代码”值

catItems.forEach(item => console.log(item.code));

Final piece of code will look like below-最后一段代码如下所示 -

const outputArray = [];
if(Object.keys(serverResp).length) {
    Object.keys(serverResp).forEach((cat)=> {
    const catItems = serverResp[cat];
    catItems.forEach(item => 
    outputArray.push(`/catalog/filter${item.code}`));
  });
 }

 console.log(outputArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM