Skip navigation
Currently Being Moderated

Get Object Property Elements and Values

Jul 4, 2012 9:29 AM

If I have an object,

 

myObject = ({a:3, b:5, c:8})

 

how can I get a list of the elements and their values according to their position

 

Let's say I want to know what the name of the second element, how can I get the answer "b"?

Also, without knowing the element name how can I get the value of the second element "5" ?

 

Thanks

 

Trevor

 
Replies
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Jul 4, 2012 10:45 AM   in reply to Trevorׅ

    This is, of course, a Javascript question, and the answer is not particular to InDesign, though I think you knew that.

     

    The short answer is: You don't. Javascript does not guarantee anything about the positioning of members of an Object.

    There is no guarantee that b will remain the second element, and there is no way to get the members of an Object by some kind of index other than their name.

     

    BTW, you write:

     

    myObject = ({a:3, b:5, c:8})

     

    Which is overly complex (extra parens) and missing a line-terminating semicolon. This is better written as:

     

    myObject = {a:3, b:5, c:8};

     

    You can iterate over the names of the keys to myObject, with for (i in myObject) { ... }, but again there are no ordering guarantees. In ExtendScript, you could use myObject.reflect.properties to return the keynames in array form, but you'll get some extra junk and it's not really consider good practice to use the reflection objects in non-debugging code.

     

    If you find yourself wanting to do this, you are probably not using Objects correctly.

    Can you tell us more about the problem you are trying to solve, please?

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 4, 2012 10:51 AM   in reply to Trevorׅ

    @Trevor – try something like that:

     

    var myObject = new Object({a:3, b:5, c:8});
     
    var position = 2;
    var counter = 0;
     
    for(x in myObject){
     
            ++counter;
            if(counter === position){
                $.writeln(x);
                $.writeln(myObject[x]);
                break;
                };
     
        };
    

     

     

    Uwe

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 4, 2012 11:05 AM   in reply to Trevorׅ

    John is correct. You cannot guarantee the order of properties in a Javascript object.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 4, 2012 2:45 PM   in reply to Trevorׅ

    ~ Trevor ~ wrote:

     

    I understand that if the properties change then so might the positions but I guess that I am correct that if the properties don't change then the positions won't either.

    No. That's not correct. Different implementations of javascript will give you different results without changing anything. Even if you seem to see consistent results, the results can change at any time.

     


    ~ Trevor ~ wrote:

     

    I also want to be able to delete some of the elements and then compare the remaining properties.

    delete obj.prop;

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points