Reputation: 71
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
Upvotes: 0
Views: 1495
Reputation: 44740
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 -
function clone(obj) {
var copy = jQuery.extend(true, {}, obj);
return copy;
}
Now call this function like this :
function dropdownchange(jsonstring)
{
var newJsonString = clone(jsonstring);
var e = document.getElementById("documents_filter");
var svalue = e.options[e.selectedIndex].value;
......
....
just use newJsonString
inside your function instead of jsonstring
.
Upvotes: 1
Reputation: 869
Store the original string in a separate variable. Use the original string variable as your function's input and then set the resulting value to a different variable.
Upvotes: 0