I encountered an issue while trying to filter json data based on user selection. The debugger console displayed the error message:
TypeError: obj.contacts.filter is not a function
. I have included a snippet of my code below:
var contactStorage = {};
$("#frm_find").on("submit", findContacts);
function findContacts(e) {
e.preventDefault();
var frmFilter = $("#frm_filterby").val();
if (frmFilter) {
$.ajax({
type: "GET",
url: "https://api.myjson.com/bins/9j7qg?" + new Date().getTime(),
dataType: "json"
}).done(function(obj) {
contactStorage = obj.contacts.filter(function(entry) {
switch (frmFilter) {
case '1':
return entry.status === 1;
break;
case '2':
return entry.status === 0;
break;
default:
return entry;
}
});
console.log(contactStorage);
}).fail(function(jqXHR, textStatus, errorThrown) {
alert('Error: ' + errorThrown);
});
}
}
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<form name="frm_find" id="frm_find" autocomplete="off">
<div class="row find-row">
<div class="form-group col-xs-12 ...
</button>
</div>
</div>
</form>
If you have any insights on resolving this issue, please share your knowledge. Thank you.