SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to enter values in to this array?

    Hi,

    I have this bit of code:

    Code JavaScript:
     var IdContainer = new Array();
         for(i=0; i < SEARCHNO; ++i) {
         	var  search_ID = xmlDoc.getElementsByTagName("searchID")[i];
       	  var SEARCHID = search_ID.firstChild.nodeValue;
     
       	  if(SEARCHID !== IDCONTAINER) {
      code to add the SEARCHID to the IDCONTAINER array here


    Now what i want it to do is every time the for loop runs the if statement checks whether the SEARCHID variable is not equal to the values in the IDCONTAINER array....if this is the case then the code after the if is run and the value in the SEARCHID variable is added to the IDCONTAINER array.

    Problem is i dont know how to structure the if statement to check all the values in the array against the SEARCHID and then i dont know how to update the array afetr the if with the new SEARCHID.


    any ideas on how to do this?

    thanks
    Last edited by elduderino; Jul 1, 2007 at 06:07.

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Phew, first off, I have to say that your variable naming scheme is incredibly confusing! You have no less than three permutations of "searchid": search_ID, searchID, and SEARCHID. And then flip flop between IdContainer and IDCONTAINER. I felt as if I were reading SQL statements.

    Here's how I would do it:

    Code JavaScript:
    var uniqueids = [];
    var searchids = xmlDoc.getElementsByTagName("searchID");
    for (var i=0;i<searchids.length;i++) {
    	var searchid=searchids[i].firstChild.nodeValue;
     
    	if (uniqueids.indexOf(searchid)==-1) uniqueids.push(searchid);
    }

    I tried to use more logical variable names and I put the searchids outside of the loop, for optimization, since you don't (I'm guessing) want to traverse the XML document's DOM each time you loop. Hope this is what you were looking for

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL, yes i know my naming conventions are a little confusing there...i dont really know what i was thinking when i wrote it

    I think i need the searchids in the loop as xml response can return many results so i need to loop through each one to get the value out. Please correct me though.

    Could i ask for clarification on this line?

    Code JavaScript:
    if (uniqueids.indexOf(searchid)==-1) uniqueids.push(searchid);
    Last edited by elduderino; Jul 1, 2007 at 10:39.

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by elduderino View Post
    LOL, yes i know my naming conventions are a little confusing there...i dont really know what i was thinking when i wrote it

    I think i need the searchids in the loop as xml response can return many results so i need to loop through each one to get the value out. Please correct me though.

    Could i ask for clarification on this line?

    [. . .]

    Im not sure why the -1 is in there. The searchid's are not always concurrent numbers, sometimes the results might return a searchid of 1 and then 18 and then 67 for example so if that code is doing some sort of check to see if the current searchid -1 matches any in the uniqueids array then it wouldnt work......but im just clutching at straws here!

    Thanks for your time
    Sure, no problem.

    When you're getting your elements, it returns something like this:

    [node,node,node,node,node]

    Where the nodevalue of each is something like:

    [1,18,67]

    as you said. But each one has a unique index:

    {0:1,1:18,2:67}

    What I am doing (and is general practice) is getting that list beforehand, and then the loop goes from 0 to searchids.length. Then searchids[i] returns, in order:

    1
    18
    67

    This is the magic condition:

    uniqueids.indexOf(searchid)==-1

    indexOf is a method of the Array prototype that will give you the first index of a value in an array. Example:

    ['shoes','figs','apricots'].indexOf('figs');

    This evaluates to to 1, because that is the index of 'figs.' Another example:

    [61,3,5].indexOf(12);

    Now this evaluates to -1 (negative 1). This is simply a JavaScript convention. It returns -1 instead of false. So if the result of our little check is -1, we know that the value isn't in the array, and we should add it. We add it like this:

    ['apples','oranges'].push('limes');

    Which just evaluates to ['apples','oranges','limes]

    This has the desired (I think) effect where you only get a list of unique search ids. If the value is already in the list, you don't add it.

    Post back if anything else isn't clear.

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im not sure why the -1 is in there but could it be that code checking to see if the current searchid has a position of -1 ( meaning that its not in the array) then if it is put that searchid in the array?

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha thats uncanny.....we must have been looking at the post at just the same time...id been looking at your code for about 20 minutes or so and it just dawned on me how it was working so i posted back.....

    Thanks for your help and the clarification..im off to try it out in my function now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •