Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Get Object Property Elements and Values

Mentor ,
Jul 04, 2012 Jul 04, 2012

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

TOPICS
Scripting
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 04, 2012 Jul 04, 2012

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-termina

...
Translate
Mentor ,
Jul 04, 2012 Jul 04, 2012

Just want to add, without using regex's

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 04, 2012 Jul 04, 2012

@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);

            break;

            };

    };

Uwe

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 04, 2012 Jul 04, 2012

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jul 04, 2012 Jul 04, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 04, 2012 Jul 04, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jul 04, 2012 Jul 04, 2012
LATEST

O.k.

I stand corrected.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 04, 2012 Jul 04, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines