解析Json与条件[closed]的对应关系

chhkpiq4  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(112)

1小时前关闭。
Improve this question
嗨,我有显示结构的JSON,只是想过滤项目对应的区域
就像如果地区是"美国",它应该显示名称对应关系,我怎么做呢?

{
    "name": "Testing",
    "category": "Sales",
    "subcategory": "Productivity",
    "imageUrl": "Banner.jpg",
    "logo": "Testing.png",
  
    "appDetails": [
      {
        "systemOverview": "https:........",
        "Benefits": [
          {
            "label": "A light weight and standalone diagnostic tool"
          }
          
        ],
        "region": [
          {
            "label": "USA"
          }
        ]
      }
    ]
  },
unftdfkk

unftdfkk1#

使用find数组方法。
它在找到第一个匹配项后停止枚举数组并返回值。

const tests = [
{
    "name": "Testing-Canada",
    "category": "Sales-Canada",
    "subcategory": "Productivity",
    "imageUrl": "Banner-Canada.jpg",
    "logo": "Testing-Canada.png",

    "appDetails": [
        {
            "systemOverview": "https:........",
            "Benefits": [
                {
                    "label": "A light weight and standalone diagnostic tool"
                }
            ],
            "region": [
                {
                    "label": "CANADA"
                }
            ]
        }
    ],
},
{
    "name": "Testing",
    "category": "Sales",
    "subcategory": "Productivity",
    "imageUrl": "Banner.jpg",
    "logo": "Testing.png",

    "appDetails": [
        {
            "systemOverview": "https:........",
            "Benefits": [
                {
                    "label": "A light weight and standalone diagnostic tool"
                }
            ],
            "region": [
                {
                    "label": "USA"
                }
            ]
        }
    ],
}
];

const findTest = ((country) => {
return result = tests.find((element) => {
    return element.appDetails.find((child) => {
        return child.region.find ((region) => {
            return region.label === country
        })
    })
});
})

console.log(JSON.stringify(findTest('USA'), null, 4));

相关问题