Copy link to clipboard
Copied
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
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
...Copy link to clipboard
Copied
Just want to add, without using regex's
Copy link to clipboard
Copied
@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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
~ 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;
Copy link to clipboard
Copied
O.k.
I stand corrected.
Copy link to clipboard
Copied
John is correct. You cannot guarantee the order of properties in a Javascript object.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more