7 Replies Latest reply: Jul 4, 2012 2:47 PM by Trevorׅ RSS

    Get Object Property Elements and Values

    Trevorׅ Community Member

      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

        • 1. Re: Get Object Property Elements and Values
          Trevorׅ Community Member

          Just want to add, without using regex's

          • 2. Re: Get Object Property Elements and Values
            John Hawkinson Community Member

            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?

            • 3. Re: Get Object Property Elements and Values
              Laubender MVP

              @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

              • 4. Re: Get Object Property Elements and Values
                Harbs. MVP

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

                • 5. Re: Get Object Property Elements and Values
                  Trevorׅ Community Member

                  John, Uwe and Harbs

                   

                  Thanks for your responses.

                   

                  Very Helpful, particularly Uwe's example

                   

                  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.

                   

                  The truth is I don't necessarily need the positions,

                  I wanted to be able to get the list of the names and elements.

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

                  I think I have all the information that I need now.

                  • 6. Re: Get Object Property Elements and Values
                    Harbs. MVP

                    ~ 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;

                    • 7. Re: Get Object Property Elements and Values
                      Trevorׅ Community Member

                      O.k.

                       

                      I stand corrected.