{"id":2117,"date":"2010-11-15T01:02:25","date_gmt":"2010-11-14T19:32:25","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2117"},"modified":"2010-11-15T10:04:58","modified_gmt":"2010-11-15T04:34:58","slug":"jquery-map-and-grep-methods","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/jquery-map-and-grep-methods\/","title":{"rendered":"Jquery : map and grep functions"},"content":{"rendered":"<p>Hi friends,<\/p>\n<p>I was going through some utility funcitons being provided by jQuery. Found few methods like grep, map very userful that saves me from writing loops. I could relate them with grep, collect respectively as provided by Groovy, thought would share with you.<\/p>\n<p>I will be taking examples with JSON objects say Student.<br \/>\n<a href=\"  http:\/\/api.jquery.com\/jQuery.grep\/\">grep()<\/a><\/p>\n<p>[javascript]<br \/>\nvar students = [ {&#8216;id&#8217;:1, &#8216;name&#8217;:&#8217;amit&#8217;},{&#8216;id&#8217;:2, &#8216;name&#8217;:&#8217;ankit&#8217;}];<br \/>\njQuery.grep(students,function(student){ return student.id&gt;1});<\/p>\n<p>\/\/Output :  [{&#8216;id&#8217;:2, &#8216;name&#8217;:&#8217;ankit&#8217;}]<\/p>\n<p>jQuery.grep(students,function(student){ return student.id&gt;1}, true) \/\/ invert the results<\/p>\n<p>\/\/Output : [ {&#8216;id&#8217;:1, &#8216;name&#8217;:&#8217;amit&#8217;}]<\/p>\n<p>[\/javascript]<\/p>\n<p><a href=\"http:\/\/api.jquery.com\/jQuery.map\/\">map() <\/a><\/p>\n<p>[javascript]<br \/>\n var students = [ {&#8216;id&#8217;:1, &#8216;name&#8217;:&#8217;amit&#8217;},{&#8216;id&#8217;:2, &#8216;name&#8217;:&#8217;ankit&#8217;}];<br \/>\njQuery.map(students,function(student){ return student.name=student.name.toUpperCase()});<\/p>\n<p>\/\/Output : [&quot;AMIT&quot;, &quot;ANKIT&quot;]<br \/>\n\/\/Updated students list :  [ {&#8216;id&#8217;:1, &#8216;name&#8217;:&#8217;AMIT&#8217;},{&#8216;id&#8217;:2, &#8216;name&#8217;:&#8217;ANKIT&#8217;}];<\/p>\n<p>jQuery.map(students,function(student){ return student.greetings=&quot;HELLO &quot; + student.name});<\/p>\n<p>\/\/Output : [&quot;HELLO AMIT&quot;, &quot;HELLO ANKIT&quot;]<br \/>\n\/\/Updated students list : [ {&#8216;id&#8217;:1, &#8216;name&#8217;:&#8217;AMIT&#8217;, &#8216;greetings&#8217; : &#8216;HELLO AMIT&#8217;},{&#8216;id&#8217;:2, &#8216;name&#8217;:&#8217;ANKIT&#8217;, &#8216;greetings&#8217; : &#8216;HELLO ANKIT&#8217;}]<br \/>\n[\/javascript]<\/p>\n<p>Hope this helped.<\/p>\n<p>~~Amit Jain~~<br \/>\namit@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi friends, I was going through some utility funcitons being provided by jQuery. Found few methods like grep, map very userful that saves me from writing loops. I could relate them with grep, collect respectively as provided by Groovy, thought would share with you. I will be taking examples with JSON objects say Student. grep() [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1],"tags":[459,27,458],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2117"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2117"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2117\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}