I'm assuming you don't really have JSON (a string) in resp
(remember, JSON is a textual notation for data exchange; if you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON), but that it's already been parsed into an array of objects (perhaps jQuery did it for you, if resp
is the response of an ajax call). (If resp
is a string containing JSON
, parse it with JSON.parse
and then continue with the rest of the answer.)
inArray
does an ===
(type and value) check. Your array contains numbers. Your object's ext
property values are strings.
Either change your array to contain strings, or perhaps convert the ext
property values to numbers as you compare:
if(jQuery.inArray(+obj.ext,arr) == -1){
// ---------------^
Also note that you'd normally use the return value of jQuery.map
for something; otherwise, you'd use jQuery.each
.
Example (keeping jQuery.map
in case you are using the result):
var resp = [{"ext":"20177","name":"SIP\/20177-000001e8","state":"Ringing"},{"ext":"20122","name":"SIP\/20122-000001e7","state":"Ringing"}];
var arr = [20122, 20133, 20144, 20155, 20177];
jQuery.map(resp, function(obj) {
if(jQuery.inArray(+obj.ext,arr) == -1){
// -------------^
console.log("Not found: " + obj.ext);
} else {
console.log("Found: " + obj.ext);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Side note: If you're using an even vaguely modern browser, you can use built-in array features rather than jQuery.map
, jQuery.each
, and jQuery.inArray
: They're the map
, forEach
, and indexOf
methods on arrays themselves. They're all polyfillable/shimmable on older browsers as well.
For example:
var resp = [{"ext":"20177","name":"SIP\/20177-000001e8","state":"Ringing"},{"ext":"20122","name":"SIP\/20122-000001e7","state":"Ringing"}];
var arr = [20122, 20133, 20144, 20155, 20177];
resp.forEach(function(obj) {
if(arr.indexOf(+obj.ext) == -1){
console.log("Not found: " + obj.ext);
} else {
console.log("Found: " + obj.ext);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>