Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    [HELP] Whacky JS array access problems!

    I have a multidimensional associative array in JS which contains category information for a navigation. It's actually set up through PHP, which pulls the info from a database then translates it into JS. I know it's set up correctly since I can see it in the source once the page is rendered.

    However, accessing this array causes external JS code (from my Slimbox files) to be displayed in the nav. It's as if the array is set up right, but accessing the elements brings in Slimbox code as the elements. It's very strange.

    You can see the effect here. It's the secondary nav under Home, Featured Items, etc. Each token that is clickable is separated by an asterisk:
    http://www.mchonejewelry.com/test/newsite/community.php

    Notice the other page's navs work great! Try the jewelry nav on the Home page and the categories are set up fine. This is because other pages do not link to Slimbox.

    (And no, the site aint pretty because I wanted to get the hard stuff working before I work on the look)

    Any clue what's going on? I have no idea how this can be happening. I've heard of jQuery interfering with Slimbox and Lightbox, but my nav is not using jQuery!

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,289
    Thanks
    12
    Thanked 345 Times in 341 Posts
    Quote Originally Posted by newmngt View Post
    I have a multidimensional associative array in JS
    there’s no such thing in JavaScript. JS Arrays are always numerical. "associative arrays" are called objects*.

    * - the difference lies in the built-in methods these objects can use.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    there’s no such thing in JavaScript. JS Arrays are always numerical. "associative arrays" are called objects*.

    * - the difference lies in the built-in methods these objects can use.
    OK, so you're saying I have created associative arrays that are objects.

    I know I can access the array and that I'm creating the array correctly. You can see on the site I've linked to that it works on other pages.

    So I don't quite get what you're saying... Are you telling me I'm doing something wrong in the way the array is being accessed?

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,289
    Thanks
    12
    Thanked 345 Times in 341 Posts
    Quote Originally Posted by newmngt View Post
    OK, so you're saying I have created associative arrays that are objects.
    nope, I’m saying that you have created Objects, that are not Arrays.

    Quote Originally Posted by newmngt View Post
    I know I can access the array and that I'm creating the array correctly. You can see on the site I've linked to that it works on other pages.
    both can be accessed in the same way, the difference is the following:
    PHP Code:
    var = [2,1,3]; // array
    alert(a[1]);
    a.sort();
    alert(a[1]);

    var 
    = {a:1,b:2,c:3}; // "associative array"
    alert(b["b"]);
    b.sort(); // <-- throws an error 
    Quote Originally Posted by newmngt View Post
    So I don't quite get what you're saying... Are you telling me I'm doing something wrong in the way the array is being accessed?
    no. just to be careful when talking about arrays, when they’re not. this may be the difference between a working script and a failure.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,289
    Thanks
    12
    Thanked 345 Times in 341 Posts
    your problem is the following:
    line #30
    Code:
    for(var menu2Cur in existingCats[menu1Selection]){
    existingCats[menu1Selection] is an Object (not an Array!) and the for-in loop cycles through all object properties, that are not built in, even if they are defined elsewhere. that includes also functions that are associated with that object.

    filtering off functions can be done this way
    PHP Code:
    for (prop in obj) {
        if (
    obj.hasOwnProperty(prop) && !(obj[prop] instanceof Function)) {
            
    // do something with obj[prop]
        
    }

    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    newmngt (03-15-2010)

  • #6
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    nope, I’m saying that you have created Objects, that are not Arrays.
    [...]
    just to be careful when talking about arrays, when they’re not. this may be the difference between a working script and a failure.
    OK, I mistyped. I am not creating associative arrays because they are not supported by javascript. Am I correct in understanding that I'm creating objects and defining properties on the fly?

    I suppose if that's the case, I'll need to think about the data structure as no longer being an array, but instead objects with properties. This still does not explain why external code is being pulled in as values, since if I am creating properties of objects, why wouldn't the code just crash?

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,289
    Thanks
    12
    Thanked 345 Times in 341 Posts
    Quote Originally Posted by newmngt View Post
    OK, I mistyped. I am not creating associative arrays because they are not supported by javascript. Am I correct in understanding that I'm creating objects and defining properties on the fly?
    correct. JavaScript is extremely flexible

    Quote Originally Posted by newmngt View Post
    I suppose if that's the case, I'll need to think about the data structure as no longer being an array, but instead objects with properties. This still does not explain why external code is being pulled in as values, since if I am creating properties of objects, why wouldn't the code just crash?
    see post above.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm typing in replies as you're replying so I'm one step behind you!

    I think I understand now. The for loop is looping over all fields in the Object, since it is not an array. Thus built-in fields that are not defined by me may be pulled in.

    Still, this does not explain how the code is working on other pages... I would think that the code would crash there too since the for loops are not correctly accessing what I think is an array.

    Regardless, this is a huge part of the problem and I really appreciate you pointing this out.

  • #9
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    filtering off functions can be done this way
    Code:
    for (prop in obj) {
        if (obj.hasOwnProperty(prop) && !(obj[prop] instanceof Function)) {
            // do something with obj[prop]
        }
    }
    I added this check to the code and it works. Really, this is a kludge, since I'm not exactly fixing the underlying problem. To truly solve this problem would involve a different code solution that didn't pretend to use associative arrays.

  • #10
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,289
    Thanks
    12
    Thanked 345 Times in 341 Posts
    I wouldn’t call it a kludge. it’s just the way, objects work. there are certainly better ways to implement that, but I suppose that’s too advanced at your current stage. the only way out I can imagine is using Arrays.

    maybe you’re interested in the Suckerfish Dropdown Menu?
    Last edited by Dormilich; 03-15-2010 at 08:48 PM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

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