I am trying to filter result from JSON string based on status selection. I will explain more in depth. In respect to below code, jsonstring contain the whole javascript object.I filter status by grep function and than i added result into main object(jsonstring) so i can get whole javascript object with new filter status. But each time this drop down called i am getting last updated javascript object but i want the original jsonstring each time this function call.
function dropdownchange(jsonstring)
{
var e = document.getElementById("documents_filter");
var svalue = e.options[e.selectedIndex].value;
if(svalue == "In Progress")
{
var docstatusfilter;
docstatusfilter = $.grep(jsonstring.Apps.statuslist , function(el, i) {
return el.DOCUMENT_STATUS === "In Progress"
});
jsonstring.Apps.statuslist = docstatusfilter;
}
if(svalue == "Auth (Verified)")
{
var authstatus;
authstatus = $.grep(jsonstring.Apps.statuslist , function(el, i) {
return el.DOCUMENT_STATUS === "Auth (Verified)"
});
jsonstring.Apps.statuslist = authstatus;
}
}
When this function call first time it works perfect but once i change dropdown it contain the previous record in jsonstring but i want the original jsonstring each time when this function call..
Anybody have any idea?
Thanks
All you need to do is copy your jsonstring into another object and use that object in your function. Here is a function that will clone your object –
Now call this function like this :
just use
newJsonStringinside your function instead ofjsonstring.