0

I have an array:

var arr = [20122, 20133, 20144, 20155, 20177];

And I have a JSON object:

[{
    "ext": "20177",
    "name": "SIP\/20177-000001e8",
    "state": "Ringing"
}, {
    "ext": "20122",
    "name": "SIP\/20122-000001e7",
    "state": "Ringing"
}]

I need to check the array as an input and check if the array values exist or not in the JSON object.

I tried like below:

var arr = [20122, 20133, 20144, 20155, 20177];
jQuery.map(resp, function(obj) {
  if(jQuery.inArray(obj.ext,arr) == -1){
    //not exist
  }                     
})

But here JSON is the input. But I need the array as input.

Venkat.R
  • 7,420
  • 5
  • 42
  • 63
raavi
  • 83
  • 10
  • what do you want to do after the check? – Nina Scholz Jul 01 '16 at 06:40
  • It's being much clear. Thanks, @nnnnnn. – Anson Jul 01 '16 at 06:41
  • you need to convert the json `ext` properies into an array usinh `map`, then you can check if any of this array exist in the `arr` array. – Mosh Feu Jul 01 '16 at 06:41
  • 1
    You don't have "a json object", because [there's no such thing as a json object.](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). Either you have JSON, which is a string that you need to parse, or you already have an array of objects. From the code shown we can't tell which because you don't show how `resp` is set. – nnnnnn Jul 01 '16 at 06:41
  • Try underscore http://underscorejs.org/#map – Venkat.R Jul 01 '16 at 06:47
  • @nnnnnn: Indeed. raavi, here's [another handy link on that](http://stackoverflow.com/a/2904181/157247). Given the name `resp`, though, we can suspect it's probably an ajax response jQuery parsed automatically. :-) – T.J. Crowder Jul 01 '16 at 06:47
  • @Venkatraman: There's no need to add another library in the above. JavaScript's own `Array#map`, `Array#indexOf`, `Array#find` and such are entirely up to the task. – T.J. Crowder Jul 01 '16 at 06:48
  • totaly agreed @T.J.Crowder. then why we need to use jQuery array functions ? – Venkat.R Jul 01 '16 at 06:49
  • @Venkatraman: In 2016? We don't. :-) They were handy in 2008, though, before there were standard semantics for these things (inspired in part by jQuery, Underscore, and PrototypeJS). – T.J. Crowder Jul 01 '16 at 06:51
  • Fine. @raavi, add your expected output in the Question – Venkat.R Jul 01 '16 at 06:52
  • @Venkatraman - Even if supporting old browsers that don't implement array `.map()` natively, there's no need to include Underscore *as well* when OP is already using jQuery. – nnnnnn Jul 01 '16 at 06:54

6 Answers6

1

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>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

If the input is JSON then you need to parse it first: var data = JSON.parse(resp);. Example bellow:

var arr = [20122, 20133, 20144, 20155, 20177];
var resp = '[{"ext":"20177","name":"SIP\/20177-000001e8","state":"Ringing"},{"ext":"20122","name":"SIP\/20122-000001e7","state":"Ringing"}]';

var data = JSON.parse(resp); //<-- You need this to convert json to array

jQuery.each(data, function(i, obj) {
  if(jQuery.inArray(obj.ext, arr) == -1){
    console.log(obj.name+' Exists');
  }                     
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Ismail RBOUH
  • 10,292
  • 2
  • 24
  • 36
1
 var arr = [20122, 20133, 20144, 20155, 20177],
resp = [{"ext":"20177","name":"SIP\/20177-000001e8","state":"Ringing"},{"ext":"20122","name":"SIP\/20122-000001e7","state":"Ringing"}],

 $.each(arr, function (i, arritem) {
            $.each(resp , function (i, jsonitem) {
               if(obj.ext==arritem)
               {
               //match
               }
               else
               {
                //not match
               }
            }
 }
1

According to your requirement "But I need the array as input" - consider the following solution using Array.forEach and Array.map functions in ES6 "manner":

var arr = [20122, 20133, 20144, 20155, 20177],
    resp = [{"ext":"20177","name":"SIP\/20177-000001e8","state":"Ringing"},{"ext":"20122","name":"SIP\/20122-000001e7","state":"Ringing"}],
    exts = resp.map((o) => +o.ext),
    result = {};

arr.forEach((v) => result[v] = (exts.indexOf(v) !== -1)? 1 : 0 );

console.log(result);
// value 1 means that arr value exists within json objects, 0 - not exists
// Object {20122: 1, 20133: 0, 20144: 0, 20155: 0, 20177: 1}
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

try this:

$('document').ready(function() {
  var arr = [20122, 20133, 20144, 20155, 20177];
  var oJson = [{"ext":"20177","name":"SIP/20177-000001e8","state":"Ringing"},{"ext":"20122","name":"SIP/20122-000001e7","state":"Ringing"}]

  $('button').on('click', function(e) {
    var newArr = [];
    
    $.each(oJson, function(key, value) {
      if ($.inArray(value.ext, arr))
        newArr.push(value.ext);
    });
    
    console.log(newArr);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button>Check</button>
Fadhly Permata
  • 1,686
  • 13
  • 26
0

I think you can use find array method & forEach to check if element exist in json

If such a json object exist , find will return the object else it will return undefined. + is used for string to number conversion , since in arr the elements are number while in jsonObj they are string

var arr = [20122, 20133, 20144, 20155, 20177];
var jsonObj=[{"ext":"20177","name":"SIP\/20177-000001e8","state":"Ringing"},{"ext":"20122","name":"SIP\/20122-000001e7","state":"Ringing"}]

arr.forEach(function(item){
 var _getValue ="";  
     var _isPresent=jsonObj.find(function(item2){
      if(item == +item2.ext){
        return item2;
       }
        else{
         return false;
        }
})
if(_isPresent !== undefined){
   _getValue =_isPresent.ext;
}
else{
  _getValue="Not matched";
}
document.write('<pre>'+_getValue+'</pre>')
})

JSFIDDLE

brk
  • 48,835
  • 10
  • 56
  • 78