0

Is there any way i can force grep function of jquery to return new object with reflected new array? For example I have sample JSON and javascript like below.

var myObject = {     "Apps" : [    
    {
        "Name" : "app1",
        "id" : "1",
        "groups" : [
            { "id" : "1", 
              "name" : "test group 1", 
              "desc" : "this is a test group"
             },
            { "id" : "2",
              "name" : "test group 2",
              "desc" : "this is another test group"
             },
              { "id" : "2",
              "name" : "test group 2",
              "desc" : "this is another test group"
             }
        ]              
    }
    ]
   }

 var b,a;
    $.each(myObject.Apps, function() {    
     b = $.grep(this.groups, function(el, i) {
    return el.id.toLowerCase() === "2"                
    });         

   }); 

 alert(JSON.stringify(b));  

So Once i run this i will get following text in alert as expected.

[{"id":"2","name":"test group 2","desc":"this is another test group"},{"id":"2","name":"test group 2","desc":"this is another test group"}]

But i want the whole javascript object with this new return array like this. Expected O/p::

 "Apps" : [    
    {
        "Name" : "app1",
        "id" : "1",
        "groups" : [
             { "id" : "2",
              "name" : "test group 2",
              "desc" : "this is another test group"
             },
              { "id" : "2",
              "name" : "test group 2",
              "desc" : "this is another test group"
             }
        ]              
    }
    ]

Any idea would be a great help.

4
  • 1
    Sure, re-write grep. Can't you just wrap it in the structure you want? Commented Jun 19, 2012 at 19:26
  • Copy object, create new object, slice elements out of object, return object. Commented Jun 19, 2012 at 19:31
  • Thanks dave. But i am not sure how can i rewrite grep. Can you give me hint? As i am wondering if i will rewrite grep than i might loose previous functionality. Commented Jun 19, 2012 at 19:33
  • 1
    @ravi I was being sarcastic--what I'm saying is keep grep the same, and wrap the results in the structure you need. Commented Jun 19, 2012 at 19:46

1 Answer 1

4

If understand correctly you want to remove any groups not returned in $.grep from the master object.

Using your $.grep() method add one line in the $.each loop after $.grep

DEMO: http://jsfiddle.net/67Bbg/

var b,a;
$.each(myObject.Apps, function() {    
   b = $.grep(this.groups, function(el, i) {
      return el.id.toLowerCase() === "2"                
   });

  /* replace group with new array from grep */         
   this.groups=b;
 });

EDIT: Abbreviated version

$.each(myObject.Apps, function() {    
    this.groups= $.grep(this.groups, function(el, i) {
      return el.id.toLowerCase() === "2"                
   });
 });
Sign up to request clarification or add additional context in comments.

2 Comments

You don't even need a and b, just assign the result of $.grep to this.groups.
@FábioBatista good point, I just was trying to fill in what I think OP was missing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.