检查json密钥的使用情况

时间:2017-10-25 06:13:33

标签: javascript json react-intl

我有一个json文件,我在几个相关的代码库中使用它(它是一个用于react-intl的语言文件)

它随着时间的推移而大幅增长(几百个键),我们想要整理它。

是否有任何快速明智的方法来确定是否有任何密钥未使用?

文件如下所示:

{
  "Account.AdditionalInformation": "Additional contact information",
  "Account.Address": "Address",
  "Account.Billing": "Billing details",
  "Account.BillingName": "Billing name",
  "Account.BillTo": "Bill to",
  "Account.BillTo.Company": "A company",
  "Account.BillTo.Me": "Me",
  "Account.CompanyName": "Company name",
  "Account.CompanyMember": "has added you to their corporate account",
  "Account.Department": "Department",
  "Account.Email": "Email address",
  "Account.EmailVerified": "Thank you for verifying your email!",
  "Account.EssentialContact": "Essential contact details",
  "Account.FamilyName": "Surname",
  "Account.GivenName": "First name",
  "Account.JobTitle": "Job title",
  "Account.Phone": "Phone number",
  "Account.Sector": "Industry sector",
  "Account.Title": "Title",
  "Account.Title.Female": "Mrs",
  "Account.Title.Male": "Mr",
  //.. and so on 
}

1 个答案:

答案 0 :(得分:0)

创建一个函数并使用hasOwnProperty来检查此对象是否具有此名称的任何键。在此之前,您需要在变量中获取json文件的内容

var myJSON = {
  "Account.AdditionalInformation": "Additional contact information",
  "Account.Address": "Address",
  "Account.Billing": "Billing details",
  "Account.BillingName": "Billing name",
  "Account.BillTo": "Bill to",
  "Account.BillTo.Company": "A company",
  "Account.BillTo.Me": "Me",
  "Account.CompanyName": "Company name",
  "Account.CompanyMember": "has added you to their corporate account",
  "Account.Department": "Department",
  "Account.Email": "Email address",
  "Account.EmailVerified": "Thank you for verifying your email!",
  "Account.EssentialContact": "Essential contact details",
  "Account.FamilyName": "Surname",
  "Account.GivenName": "First name",
  "Account.JobTitle": "Job title",
  "Account.Phone": "Phone number",
  "Account.Sector": "Industry sector",
  "Account.Title": "Title",
  "Account.Title.Female": "Mrs",
  "Account.Title.Male": "Mr",
  //.. and so on 
}

function checkKey(keyName) {

  if (myJSON.hasOwnProperty(keyName)) {

    alert(keyName + " Exists")
  }
}
checkKey('Account.Address')