Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching 2 JSON objects

I have a very large json like :

raw_obj= {"001" : {....}, "002" : {....}};

and I have an another json object which is just returned from server :

search_result = {["001", "005", "123"]};

I want to do something like

$.each(search_result, function(i,val){
 alert(raw_obj.search_result[i]);
});

Is it possible? I don't want to loop through those 2 objects because in practical, there will be have around 2000 elements in a "raw_json". Which means the worst case is 2000x2000 times loop per one query.

like image 321
lngs Avatar asked Jun 17 '26 00:06

lngs


1 Answers

var raw_obj= {"001" : {'...'}, "002" : {'...'}};
var search_results = ["001", "005", "123"];    // just an array

$.each(search_results, function(i, result) {
    alert(raw_obj[result]);
});

The search results are an array (ie, list), not an object (ie, map) and so the syntax should be modified as above. If you have no control over the server response, use string parsing to build a new array.

like image 110
Joe Coder Avatar answered Jun 18 '26 13:06

Joe Coder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!