I am currently working on developing an application that can function offline. My data is stored in a local JSON file and I am looking to create a filter for this data without having it hardcoded in a JavaScript file. The examples I have come across so far only involve hardcoding the filter criteria.
Here is a brief snippet of my JSON file:
[
{
"ID": 1 ,
"name": "sub conjunctival haemorrhage",
"score" : 1,
"surgeryType": ["scleral buckle", "general", "ppv"]
},
{
"ID": 2,
"name": "dry eye",
"score": 5,
"surgeryType":["general", "pr"]
}
]
The filter provided below does function, but I am seeking a way to implement this filter without embedding the data directly into the JavaScript file. How can I search by 'surgery type' while keeping the JSON data within a separate JSON file instead of being hardcoded as shown below?
<script>
var jsonText = '[
{
"ID":1,
"name":"sub conjunctival haemorrhage",
"score":1,
"surgeryType":["scleral buckle","general","ppv"]},
{
"ID":2,
"name":"dry eye",
"score":5,
"surgeryType":["scleral buckle","pr"]}]';
var jsonArray = JSON.parse(jsonText);
var surgerySearchTerm = "scleral buckle";
var filtered = jsonArray.filter(jsonObject =>
jsonObject.surgeryType.includes(surgerySearchTerm));
console.log("Filtered below");
console.log(filtered);
</script>