I have a JSON file with various file names and extensions:
{
"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"
}
I am trying to implement a search functionality where if the user types 'xls' in a search bar, it should return the matching key-value pairs like this:
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
Currently, I am using JSON.parse
and JSON.stringify
, but I'm facing issues as I have to specify keys like
json_file.CONTRACT_LIST_EXPORT_FILENAME
which returns lowercase values such as contracts.xls
. Can you suggest a workaround for this problem?
After taking advice from @nikhil, I have updated my code as follows:
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);