首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用嵌套子搜索遍历嵌套JSON

使用嵌套子搜索遍历嵌套JSON
EN

Stack Overflow用户
提问于 2017-12-22 03:13:01
回答 3查看 146关注 0票数 0

我有一个JSON结构,如下所示:

代码语言:javascript
复制
"benefitValues" : [ {
          "changeDate" : "2017-10-13T20:26:13.000+0000",
          "changeUserName" : "aaaa",
          "numericValue" : 20,
          "value" : "20",
          "amountType" : {
            "allowCustomDataFlg" : false,
            "dataType" : "Percent",
            "defaultTypeFlg" : true,
            "defaultValue" : "Unlimited",
            "description" : null,
            "maxValue" : null,
            "minValue" : null,
            "name" : "LIST",
            "benefit" : {
              "category" : "Facility Services",
              "name" : "Single Limit",
              "networkStatus" : "IN_NETWORK",
              "planType" : "MedicalPlan",
              "sortOrder" : 20,
              "subcategory" : "Acupuncture Treatment",
              "subcategorySortOrder" : 6
            }
          }
        }]

基于字符串“针灸治疗”,我需要提取值和数据类型。数据集非常大,有数百个子类别。我找不到一个好的方法来搜索这些数据。我尝试了json-path和advanced json-path,但是如果我搜索子元素,就无法返回父元素。我希望我的输出如下所示:

代码语言:javascript
复制
{
          "Subcategory" : "Acupuncture Treatment",
          "Value" : "20",
          "Type" : "Percent"
}

我希望有一种简单的方法来使用现有的库来做到这一点,或者至少使用一个简单的循环。

EN

回答 3

Stack Overflow用户

发布于 2017-12-22 03:25:21

这将从benefitValues中找到匹配的元素,并将该元素转换为您期望的格式:

代码语言:javascript
复制
var benefitValues = [{
  "changeDate": "2017-10-13T20:26:13.000+0000",
  "changeUserName": "aaaa",
  "numericValue": 20,
  "value": "20",
  "amountType": {
    "allowCustomDataFlg": false,
    "dataType": "Percent",
    "defaultTypeFlg": true,
    "defaultValue": "Unlimited",
    "description": null,
    "maxValue": null,
    "minValue": null,
    "name": "LIST",
    "benefit": {
      "category": "Facility Services",
      "name": "Single Limit",
      "networkStatus": "IN_NETWORK",
      "planType": "MedicalPlan",
      "sortOrder": 20,
      "subcategory": "Acupuncture Treatment",
      "subcategorySortOrder": 6
    }
  }
}];

// Find the element
let treatment = benefitValues.find((item) => item.amountType.benefit.subcategory === 'Acupuncture Treatment');

let result = {
  Value: treatment.value,
  Subcategory: treatment.amountType.benefit.subcategory,
  Type: treatment.amountType.dataType
}

console.log(result);

票数 0
EN

Stack Overflow用户

发布于 2017-12-22 03:29:30

您可以使用.filter搜索您的数据集并仅提取与您的字符串匹配的项。这将为您提供整个对象,因此您可以使用.map将其转换为您想要的结构。

或者,如果您只对第一个结果感兴趣,则可以使用.find

代码语言:javascript
复制
const json = {"benefitValues" : [{
  "changeDate" : "2017-10-13T20:26:13.000+0000",
  "changeUserName" : "aaaa",
  "numericValue" : 20,
  "value" : "20",
  "amountType" : {
    "allowCustomDataFlg" : false,
    "dataType" : "Percent",
    "defaultTypeFlg" : true,
    "defaultValue" : "Unlimited",
    "description" : null,
    "maxValue" : null,
    "minValue" : null,
    "name" : "LIST",
    "benefit" : {
      "category" : "Facility Services",
      "name" : "Single Limit",
      "networkStatus" : "IN_NETWORK",
      "planType" : "MedicalPlan",
      "sortOrder" : 20,
      "subcategory" : "Acupuncture Treatment",
      "subcategorySortOrder" : 6
    }
  }
}]};

// With filter/map
const result = json.benefitValues
  .filter(val => val.amountType.benefit.subcategory === "Acupuncture Treatment")
  .map(val => ({Subcategory: val.amountType.benefit.subcategory, Value: val.value, Type: val.amountType.dataType}));
  
console.log(result)

// With find / manual transform:
const singleFullResult = json.benefitValues
  .find(val => val.amountType.benefit.subcategory === "Acupuncture Treatment")

const singleResult = {
  Subcategory: singleFullResult.amountType.benefit.subcategory,
  Value: singleFullResult.value,
  Type: singleFullResult.amountType.dataType
}

console.log(singleResult)

票数 0
EN

Stack Overflow用户

发布于 2017-12-22 03:34:16

您可以结合使用Array.prototype.filter()Array.prototype.map(),并创建具有所需结构的对象数组。下面是一个例子:

代码语言:javascript
复制
let myArray = [{
    "changeDate": "2017-10-13T20:26:13.000+0000",
    "changeUserName": "aaaa",
    "numericValue": 20,
    "value": "20",
    "amountType": {
        "allowCustomDataFlg": false,
        "dataType": "Percent",
        "defaultTypeFlg": true,
        "defaultValue": "Unlimited",
        "description": null,
        "maxValue": null,
        "minValue": null,
        "name": "LIST",
        "benefit": {
            "category": "Facility Services",
            "name": "Single Limit",
            "networkStatus": "IN_NETWORK",
            "planType": "MedicalPlan",
            "sortOrder": 20,
            "subcategory": "Acupuncture Treatment",
            "subcategorySortOrder": 6
        }
    }
}];

let ret = myArray
                 .filter(arr => arr.amountType.benefit.subcategory === 'Acupuncture Treatment')
                 .map(arr => {
                         return {
                             Subcategory: arr.amountType.benefit.subcategory,
                             Value: arr.value,
                             Type: arr.amountType.dataType
                         };
                  });
console.log(ret);

首先,filter函数将过滤您的数组并仅返回与“针灸疗法”相关的项,然后map函数将只返回您需要的字段。map函数接收一个将对数组中的每个项执行的函数作为参数,并将返回一个新的结构。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47931367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档