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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accessing an object within an array nested within another object.

    I'm writing a program that involves a network of interconnected nodes (or simply objects in my example below). It depends on being able to access properties of an object's linked objects (a bit oddly worded, sorry)...

    Problem is I'm not sure how to properly access those properties... see below please.

    <script>

    //This is an example of a problem im having in my own code...
    //I want to access the name of the object within the links array wintin the object...

    var objA = {name: "Object A", links: [objB, objC]};
    var objB = {name: "Object B", links: [objC, objD, objE]};
    var objC = {name: "Object C", links: [objB]};
    var objD = {name: "Object D", links: [objE]};
    var objE = {name: "Object E", links: [objD]};

    //ex: I want to access the name of Object A's first link...
    console.log(objA.links[0].name);

    </script>

    I'm hoping to get "Object B"...
    But instead I get: TypeError: Result of expression 'objA.links[0]' [undefined] is not an object.
    Is there another way around this? Any thoughts are appreciated.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Uh well , probabley not what
    you are after but it gives the
    results you wanted that is ,
    "Object B"...
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="daveyerwin">
    <title>Untitled</title>
    <script>
    var objA = {name: "Object A", links: ['objB', 'objC']};
    var objB = {name: "Object B", links: ['objC', 'objD', 'objE']};
    var objC = {name: "Object C", links: ['objB']};
    var objD = {name: "Object D", links: ['objE']};
    var objE = {name: "Object E", links: ['objD']};
    
    
    alert(window[objA.links[0]].name)
    
    </script>
    </head>
    <body>
    
    </div>
    </html>

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,947
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    I assume you are aware that if you simply changed the order of your code it would work:
    Code:
    var objB = {name: "Object B", links: [objC, objD, objE]};
    var objA = {name: "Object A", links: [objB, objC]};
    console.log(objA.links[0].name);
    But of course, that doesn't help get links[1] or links[2] and there's no ordering of code that works for objD and objE.

    But you know, you could always do this:
    Code:
    var objA = {name: "Object A" };
    var objB = {name: "Object B" };
    var objC = {name: "Object C" };
    var objD = {name: "Object D" };
    var objE = {name: "Object E" };
    objA.links = [objB, objC];
    objB.links = [objC, objD, objE];
    objC.links = [objB];
    objD.links = [objE];
    objE.links = [objD];
    
    document.write( objA.links[0].name );
    It's not as neat and clean, but...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    That works..

    Thank you for your replies!

    I think I'll have to change my code to add the links property after and all should work well. Thanks for your time with this!!


  •  

    Posting Permissions

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