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 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    loop throw Object property

    Hello list
    here is the thing i have simple object :
    ------------------------------------------------
    function struct()
    {
    this.v_name;
    this.next_obj;
    }
    no in the body i have something like this:
    ------------------------------------------------
    var p=new struct();
    var q=new struct();
    var z=new struct();
    p.v_name="blah_p";
    q.v_name="blah_q";
    z.v_name="blah_z";
    z.next_obj=null;
    q.next_obj=z;
    p.next_obj=q;


    -----------------------------------------------
    my question is , is there any way to loop throw the objects hierarchy to check every object v_name value. in other words to automate the process of checking manually like this :
    p.next_obj.next_obj.v_name .....and so on ..
    to build some kind of function to loop throw the first "p" object .next_obj property
    and alert every .v_name along the way..
    hope you got my point here
    thanks!

  • #2
    New Coder
    Join Date
    Jun 2002
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi

    it depends on what you mean: if you want to loop by javaScript all the properties of ONE object, use for-in loops:

    for(var aVarName in YOUROBJECT){
    //aVarName inside here is the NAME of the attribute
    //YOUROBJECT is the object
    //YOUROBJECT[aVarName] is the value of the currently looped attribute
    }

    http://www.unitedscripters.com/scripts/array8.html
    towards bottom file has a funny documentation on this.

    If on the other hand you want to concatenate all your objects so that you can loop the OBJECTS themselves, and by reteriving the object then mayube their properties, you have to build a concatenation of them.
    Easiest way? An hardcoded array:

    var allMyObjects= new Array(v_name, z_name, x_name);

    Obviuosly, populate it AFTER you've defined your variables!

    then you can build/custom your own fucntions to scan in the fashions that best suit your need:

    for(var i=0; i<allMyObjects.length;i++){
    for(var i2 in allMyObjects[i]){
    //allMyObjects[i][i2] is one property value
    }
    }

    Also, the way you use the struct func isn't much practical: pass arguemtns to it:

    function struct(name1, name2){
    this.name1=name1;
    this.name2=name2;
    }

    var v_name=new struct("itsname1", "wow").

    I also noticed the name struct. It is a good choice, and the approach you follow is fine.
    I just get the chance to champion one "cause": javaScript is beautiful, like other scripting languages as PHP, for it cumulates the best of both words: static functions world, and OOP world. It should stay like that, and never converto to strongly data typed languages as its ONLY shape: as a scripting saying of mine goes:

    "when scripting, never specualte. And if you do it anyway, do it in order to INCLUDE possibilities, never to exclude them". But this is another story than what you asked!

    ciao
    Alberto http://www.unitedscripters.com/

  • #3
    New Coder
    Join Date
    Jun 2002
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello tnx for the fast reply
    well you gave me some ideas but its not exactly the thing i meant to accomplish
    the final propos is to mimic the the linked chain(or list) that mainly used in c .

    so what we got here .. we all know how to build the struct()

    -----------------------------------------------------------------
    function struct()
    {
    this.v_name;
    this.next_obj;
    }
    ------------------------------------------------------------------
    here the "next_obj" property will hold/point to the next struct() object
    and so on ( the last struct() object next_obj will hold null that means end of list)

    ok for now ..
    now im gona init the linked chain
    -------------------------------------------------------------------
    var p=new struct();
    var q=new struct();
    var z=new struct();
    p.v_name="blah_p";
    q.v_name="blah_q";
    z.v_name="blah_z";
    z.next_obj=null;
    q.next_obj=z;
    p.next_obj=q;
    -------------------------------------------------------------------
    ok i have linked chain now...

    now is my problem is say i like only to get the ".v_name" value of every elm in the
    linked chain .well here you gave good idea of using the function :
    ----------------------------------------------------------------------
    for(var i=0; i<allMyObjects.length;i++){
    for(var i2 in allMyObjects[i]){
    //allMyObjects[i][i2] is one property value
    }
    }
    -------------------------------------------------------------------------
    but there is few problems here first of all i like to get only one property in every elm in the list
    the (v_name , well in case i will have more then 1 property myebe if i will have 5 so i will like to get only 2 or 3 )
    and the second thing allMyObjects array how can i arrange the right order of the elements so it will
    be in the right order ?

    p.s
    still i did not read carefully the link you gave me but i saw there is something about hase table
    sound interesting , and of course im gona do some thinking about the subject i talked about but i will be happy to hear more ideas

    thanks!
    p.s2
    did i make my self clear this time? i have this problem that English is not my native
    lang so its kinda hard to express , but im doing my best

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    no one has any direction?

    im still breaking my head here ..
    or maybe i was not understood

  • #5
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72 W. 48' 57" , 41 N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    i think i know what you're talking about, but i'm not quite sure. i came up with the following, based on what i think you want.

    Code:
    var p=new struct();
    var q=new struct();
    var z=new struct();
    p.v_name="blah_p";
    q.v_name="blah_q";
    z.v_name="blah_z";
    z.next_obj=null;
    q.next_obj=z;
    p.next_obj=q;
    
    var v_nameArray = new Array();
    
    function listWalker(objekt) {
    v_nameArray[v_nameArray.length] = objekt.v_name;
    if (objekt.next_obj != null) { listWalker(objekt.next_obj); }
    }
    that will go through your linked list, in order, and take ever v_name property on the list, and store it in an array.

    if that isn't what you want, then what's your native language? a few of this board's members, myself included, understand german, and i'm sure some of the others speak other languages, so go ahead and try explaining in your mother tongue. can't hurt.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #6
    New Coder
    Join Date
    Jun 2002
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    recursion ....

    great direction you gave me
    tnx

  • #7
    New Coder
    Join Date
    Jun 2002
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi

    yes I got you wanted a linked list, but that's not difficult to implement so I thought the point might have been another one.
    By and large to get a linked list all you have to do is to link the object in the line upon calling in the constructor (in languages other than javaScript) or after you've called in the constructor a first time, also if you haven't populated yet all its properties values (javaScript way):

    function struct(nextObj, debug)
    {
    this.debug=debug; /*you can delete this property later on*/
    this.v_name;
    this.next_obj=nextObj || false;
    }

    var a=new struct(0, "b");b=new struct(0, "c"); c=new struct(0, "none");
    /*that is, initialize the vars like new struct, without linking them yet for a static function that does that -see below- spares you time and typos. Also, if they are not objects, or initialized as such inside the next function, javascript might not make the pointers.
    pointers in javascript:
    http://www.unitedscripters.com/spell.../pointers.html
    */

    function linked(){
    if(!linked.arguments.length){return false};
    for(var L=0;L<linked.arguments.length-1;L++){
    var x=linked.arguments[L];
    x.next_obj=linked.arguments[L+1];
    }
    }

    //ok, let's test it:
    linked(a,b,c);
    alert(a.debug+"\n"+b.debug+"\n"+c.debug)

    I hope this helps
    ciao
    Alberto http://www.unitedscripters.com/

  • #8
    New Coder
    Join Date
    Jun 2002
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Great idea !

    Great idea !
    i did not know about the arguments Object
    but then again i find every day something new in this
    Great js ....
    tnx


  •  

    Posting Permissions

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