Ibrahim İnce Ibrahim İnce - 2 months ago 5
JSON Question

Unable to return obj with two variables

The following jQuery is working successfully. What I am trying to do is to search by

Id
as well. To be more specific. I would like to search by
Name
or
Id
.
Any suggestions?

$(function() {
$("#KUNDE").focus();
});

var uri = 'json.json';

function find() {
var info = $('#KUNDE').val();
$.getJSON(uri).done(function(data) {
var item = data.filter(function(obj) {
return obj.name === info;
//return obj.name,ID === info;
//return { obj: name, obj:ID } === info;
//return ['ID', 'name'] == info;
})[0];
if (typeof item !== 'undefined' || item !== null) {
$("ul").append("<li>" + 'ID = ' + item.ID, 'Name = ' + item.name, "<br />" + 'Phone = ' + item.phone, "<br />" + 'Contact = ' + item.contact, "<br />" + 'BalanceLCY = ' + item.balanceLCY, "<br />" + 'CreditLimitLCY = ' + item.creditLimitLCY, "</li>")
}
}).fail(function(jqXHR, textStatus, err) {
$('#RESULTS').text('Error: ' + err);
});
}

var options = {
url: "json.json",
getValue: "name",
list: {
match: {
enabled: true
}
},
theme: "square"
};

$("#KUNDE").easyAutocomplete(options);

Answer

You just need to use an 'or' condition in your filter logic, like this:

var item = data.filter(function(obj) {
    return obj.name === info || obj.ID === info;
}).get(0);
Comments