如何像浏览数据库一样浏览 JSON 产品?

How to go through JSON product like it is your database?

我有一个 JSON 文件,如下所示:

{
  "COMPONENTS_EXPORT_FILENAME" : "Components.xls",
  "CONTRACT_DETAILS_EXPORT_FILENAME" : "ContractDetails.xls",
  "CONTRACT_LIST_EXPORT_FILENAME" : "Contracts.xls",
  "EQUIPMENTS_EXPORT_FILENAME" : "EquipmentList.xls",
  "FILENAME_PDF_SERVICE_PERFORMANCE_REPORT" : "ServicePerformanceReport.pdf",
  "INVOICES_CONTRACT_LIST_EXPORT_FILENAME" : "ContractInvoices.xls",
  "INVOICES_LIST_EXPORT_FILENAME" : "ServiceInvoice.xls",
  "INVOICES_PSR_LIST_EXPORT_FILENAME" : "PSR_Invoices.xls",
  "MG_REQUEST_TYPE_OPTION_VALUES" : "PMReq:#Preventive Maintenance" 
  "OPTIONS_LIST_EXPORT_FILENAME" : "OptionsList.xls"
}

我想创建一个搜索栏,如果我输入:xls

应该return我:

OPTIONS_LIST_EXPORT_FILENAME: OptionsList.xls
COMPONENTS_EXPORT_FILENAME : Components.xls
CONTRACT_DETAILS_EXPORT_FILENAME : ContractDetails.xls
CONTRACT_LIST_EXPORT_FILENAME : Contracts.xls
EQUIPMENTS_EXPORT_FILENAME" : EquipmentList.xls

等等

我正在使用 JSON.parseJSON.stringify

但我必须使用特定名称,例如 json_file.CONTRACT_LIST_EXPORT_FILENAME

它给了我 contracts.xls

请提出解决方法。

在@nikhil 的评论之后我想出了这个代码:

    var data = {
   "COMPONENTS_EXPORT_FILENAME" : "Components.xls",
   "CONTRACT_DETAILS_EXPORT_FILENAME" : "ContractDetails.xls",
   "CONTRACT_LIST_EXPORT_FILENAME" : "Contracts.xls",
   "EQUIPMENTS_EXPORT_FILENAME" : "EquipmentList.xls",
  "FILENAME_PDF_SERVICE_PERFORMANCE_REPORT" : 
  "ServicePerformanceReport.pdf",
 "INVOICES_CONTRACT_LIST_EXPORT_FILENAME" : "ContractInvoices.xls",
 "INVOICES_LIST_EXPORT_FILENAME" : "ServiceInvoice.xls",
 "INVOICES_PSR_LIST_EXPORT_FILENAME" : "PSR_Invoices.xls",
 "MG_REQUEST_TYPE_OPTION_VALUES" : "PMReq:#Preventive Maintenance",
 "OPTIONS_LIST_EXPORT_FILENAME" : "OptionsList.xls"
 };


function getTextInput() {
        var e = document.getElementById("myText").value;
        document.getElementById("txt").innerHTML = e;
        return e;
        }

function search(data, searchKey) {
var results = [];

Object.entries(data).forEach(([key, value]) => {
  if (key.toLowerCase().includes(searchKey.toLowerCase()) ||
  value.toLowerCase().includes(searchKey.toLowerCase())) {
  results.push({
    [key]: [value]
  });
 }
});

 for(var i in results)
{
  document.write(i + "=" + results[i] + '<br>');
}


}
   search(data,getTextInput);

您只能对包含给定搜索字符串的 return 个条目使用 Object.entries() and Array.filter()

var data = {
  "COMPONENTS_EXPORT_FILENAME" : "Components.xls",
  "CONTRACT_DETAILS_EXPORT_FILENAME" : "ContractDetails.xls",
  "CONTRACT_LIST_EXPORT_FILENAME" : "Contracts.xls",
  "EQUIPMENTS_EXPORT_FILENAME" : "EquipmentList.xls",
  "FILENAME_PDF_SERVICE_PERFORMANCE_REPORT" : "ServicePerformanceReport.pdf",
  "INVOICES_CONTRACT_LIST_EXPORT_FILENAME" : "ContractInvoices.xls",
  "INVOICES_LIST_EXPORT_FILENAME" : "ServiceInvoice.xls",
  "INVOICES_PSR_LIST_EXPORT_FILENAME" : "PSR_Invoices.xls",
  "MG_REQUEST_TYPE_OPTION_VALUES" : "PMReq:#Preventive Maintenance",
  "OPTIONS_LIST_EXPORT_FILENAME" : "OptionsList.xls"
};


function search(data, searchKey) {
  return Object.entries(data).filter(([key, value]) => {
    return key.toLowerCase().includes(searchKey.toLowerCase()) ||
      value.toLowerCase().includes(searchKey.toLowerCase());
  });
}

console.log(search(data, ".xls"));