I am attempting to use JavaScript to filter a JSON field based on a string input. Essentially, I have a search box and a simulated JSON response. When I type letters into the search box, an ajax call should filter my simulated response based on the input string so that I can display the result.
My search box and function to make the ajax call with the simulated response are working correctly. However, I am encountering difficulties when it comes to filtering.
var res = response.filter(function (i, n) {
return String(n.Name).toLowerCase().indexOf(String(srt).toLowerCase()) === 0;
});
Below is the simulated JSON response:
[{ "Name": "ALICE", "Close": 7.12, "UpDown": 1 }, { "Name": "MICHAEL", "Close": 110.78, "UpDown": 1 }]
n.Name
is returning undefined
. As a result, the res
contains objects with undefined
values. In this scenario, if I enter und
, for instance, all elements are displayed; but if I enter any other value, none of the elements pass the filter.
Why is this happening and how can I resolve this issue?