Click here to Skip to main content
15,917,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

In my javascript i want to iterate the value. the code is:

JavaScript
function TicketList()
    {
        var self=this;
        self.sample = ko.observableArray([]);
        self.Getjobs = function () {
            
            $.getJSON("/api/ticket", function (data) {
                $.each(data, function (key, val) {
                        
                        self.sample.push(new Ticket(val.JobID, val.CreatedDateTime,val.JobTitle));
                  
                });


            });
        };
    }


here sample containing a jason array list in looping time i want to check the val is containing in the sample.if containing ,dont add data,otherwise add the data to sample.
Posted

Try with jQuery.inArray()[^].

Something like below...
JavaScript
if(jQuery.inArray(val, self.sample) > 0)
{
    // Exists.
}
else
{
    // Don't exist.
}

But check what exactly is saved in array, if the val is stored in array, then it can be searched directly, otherwise just modify the code accordingly.
 
Share this answer
 
Comments
josh-jw 5-Aug-13 3:41am    
do you know how to do it in knockout.js?
josh-jw 5-Aug-13 4:20am    
In that example i am getting id as undefined.
Which id? Please check your code thoroughly, if there any errors or not.
There isn't any specific way to compare objects in Javascript. So, if you are sure that the objects will be having same key-value pairs, you can use JSON.stringify() and compare the outcome. But, it may not be the exact solution to this problem.

So, here iteration will be better for the check. For that you can use the following code -
JavaScript
function itemExists(targetArray, targetObject) {
  $.each(targetArray, function(i, item){
    if(item.jobID === targetObject.jobID) {
      return true;
    }
    //if job id is not unique then we need to compare all the values
  });
  return false;
}

function TicketList()
{
        var self=this;
        self.sample = ko.observableArray([]);
        self.Getjobs = function () {

            $.getJSON("/api/ticket", function (data) {
                $.each(data, function (key, val) {

                        var newTicket = new Ticket(val.JobID, val.CreatedDateTime,val.JobTitle);

                        //if doesnot exist push into the array
                        if(!itemExists(self.sample, newTicket)) {
                             self.sample.push(newTicket);
                        }
                });
            });
        };
}
 
Share this answer
 
v2
Comments
josh-jw 5-Aug-13 3:41am    
do you know how to do it in knockout.js?
Ashutosh Mahto 5-Aug-13 22:03pm    
I think this is a nice way you are doing to populate the models. Even then you can use knockout utility functions i.e. functions in ko.utils. I haven't used knockout for this purpose but may be following code can help you -

var mappedData = ko.utils.arrayMap(dataFromServer, function(item) {
//here you can make a check for items to be duplicate
return new Item(item.name, item.category, item.price);
});

Please refer to - http://www.knockmeout.net/2011/04/utility-functions-in-knockoutjs.html, this may help you.

Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900