亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

過濾掉嵌套對象并返回一個新對象

過濾掉嵌套對象并返回一個新對象

翻閱古今 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 關注
  • 85 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號