【问题标题】:Filter out a nested object and return a new object过滤掉嵌套对象并返回一个新对象
【发布时间】:2020-11-01 07:52:51
【问题描述】:

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

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

每个项目(productNameproductDescription 等)都包含键值对,其中键是语言代码,值是该语言的相关文本。我想过滤掉所有没有具有“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/"
  }
}

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

【问题讨论】:

标签: javascript object filter nested


【解决方案1】:

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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    相关资源
    最近更新 更多