为了账号安全,请及时绑定邮箱和手机立即绑定

过滤掉嵌套对象并返回一个新对象

过滤掉嵌套对象并返回一个新对象

翻阅古今 2022-11-11 13:46:56
我有一个结构如下的对象:{  "productName": {    "de-DE": "Hudson",    "en-US": "Hudson Wall Cup",    "fr-FR": "Hudson Wall Cup FR"  },  "productDescription": {    "en-US": "Wall Hanging Glass Flower Vase and Terrarium",    "it-IT": "Wall Hanging Glass Flower Vase and Terrarium IT"  },  "sizetypecolor": {    "en-US": "3 x 3 x 5 inches; 5.3 ounces"  },  "image": {    "en-US": [      {        "sys": {          "type": "Link",          "linkType": "Asset",          "id": "Xc0ny7GWsMEMCeASWO2um"        }      }    ],    "it-IT": [      {        "sys": {          "type": "Link",          "linkType": "Asset",          "id": "Xc0ny7GWsMEMCeASWO2um"        }      }    ]  },  "tags": {    "en-US": ["vase", "flowers", "accessories", "translation"],    "jp": ["vase", "flowers", "accessories", "translation"]  },  "website": {    "en-US": "http://www.amzon.com/dp/B00E82D7I8/"  }}每个项目(productName等productDescription)都包含键值对,其中键是语言代码,值是该语言的相关文本。我想过滤掉所有没有“en-US”键的嵌套键值对,因此返回以下对象:{  "productName": {    "en-US": "Hudson Wall Cup"  },  "productDescription": {    "en-US": "Wall Hanging Glass Flower Vase and Terrarium"  },  "sizetypecolor": {    "en-US": "3 x 3 x 5 inches; 5.3 ounces"  },  "image": {    "en-US": [      {        "sys": {          "type": "Link",          "linkType": "Asset",          "id": "Xc0ny7GWsMEMCeASWO2um"        }      }    ]  },  "tags": {    "en-US": ["vase", "flowers", "accessories", "translation"]  },  "website": {    "en-US": "http://www.amzon.com/dp/B00E82D7I8/"  }}过滤掉非嵌套对象有很多有用的答案,但我一直无法找到适用于这种嵌套结构的解决方案。过滤掉不必要的键值对的最佳方法是什么?
查看完整描述

1 回答

?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

const data = {

    "productName": {

      "de-DE": "Hudson",

      "en-US": "Hudson Wall Cup",

      "fr-FR": "Hudson Wall Cup FR"

    },

    "productDescription": {

      "en-US": "Wall Hanging Glass Flower Vase and Terrarium",

      "it-IT": "Wall Hanging Glass Flower Vase and Terrarium IT"

    },

    "sizetypecolor": {

      "en-US": "3 x 3 x 5 inches; 5.3 ounces"

    },

    "image": {

      "en-US": [

        {

          "sys": {

            "type": "Link",

            "linkType": "Asset",

            "id": "Xc0ny7GWsMEMCeASWO2um"

          }

        }

      ],

      "it-IT": [

        {

          "sys": {

            "type": "Link",

            "linkType": "Asset",

            "id": "Xc0ny7GWsMEMCeASWO2um"

          }

        }

      ]

    },

    "tags": {

      "en-US": ["vase", "flowers", "accessories", "translation"],

      "jp": ["vase", "flowers", "accessories", "translation"]

    },

    "website": {

      "en-US": "http://www.amzon.com/dp/B00E82D7I8/"

    }

  }


function filter(object) {

    return Object.entries(object).reduce((filtered, [key,val]) => {

        if(typeof val === "object" && !Array.isArray(val)) filtered[key] = filter(val);

        if(key === "en-US") filtered[key] = val; 

        return filtered;      

    },{})

}


console.log(filter(data))


查看完整回答
反对 回复 2022-11-11
  • 1 回答
  • 0 关注
  • 55 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信