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.
grepthe same, and wrap the results in the structure you need.