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

我有一个结构如下的对象:


{

  "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/"

  }

}

过滤掉非嵌套对象有很多有用的答案,但我一直无法找到适用于这种嵌套结构的解决方案。过滤掉不必要的键值对的最佳方法是什么?


翻阅古今
浏览 57回答 1
1回答

犯罪嫌疑人X

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))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript