0

I am just trying to create an Object containg several Controls from After Effects ScriptUI API.

This is what my obj looks like:

var easeyPeasy = {};

// groups
easeyPeasy.groups = {
  "easeIn_grp": ease__Panel.add('group', undefined, {
    name: 'easeIn group'
  }),
  "easyEase_grp": ease__Panel.add('group', undefined, {
    name: 'easyEase group'
  }),
  "easeOut_grp": ease__Panel.add('group', undefined, {
    name: 'easeOut group'
  })
};

// labels
easeyPeasy.label = {
  "easeIn_label": easeyPeasy.groups["easeIn_grp"].add('statictext', undefined, 'ease in:', {
    name: 'easeIn_label'
  }),
  "easyEase_label": easeyPeasy.groups["easyEase_grp"].add('statictext', undefined, 'easy ease: ', {
    name: 'easyEase_label'
  }),
  "easeOut_label": easeyPeasy.groups["easeOut_grp"].add('statictext', undefined, 'ease out:', {
    name: 'easeOut_label'
  })
};

// slider
easeyPeasy.slider = {
  "easeIn_slider": easeyPeasy.groups["easeIn_grp"].add('slider', undefined, 50, 0, 100, {
    name: 'easeIn_slider'
  }),
  "easyEase_slider": easeyPeasy.groups["easyEase_grp"].add('slider', undefined, 50, 0, 100, {
    name: 'easyEase_slider'
  }),
  "easeOut_slider": easeyPeasy.groups["easeOut_grp"].add('slider', undefined, 50, 0, 100, {
    name: 'easeOut_slider'
  })
}

// inputs
easeyPeasy.inputs = {
  "easeIn_input": easeyPeasy.groups["easeIn_grp"].add('edittext', undefined, 50, {
    name: 'easeIn_input'
  }),
  "easyEase_input": easeyPeasy.groups["easyEase_grp"].add('edittext', undefined, 50, {
    name: 'easyEase_input'
  }),
  "easeOut_input": easeyPeasy.groups["easeOut_grp"].add('edittext', undefined, 50, {
    name: 'easeOut_input'
  })
}

if I know try to return the length of my easeyPeasy.inputs for example I only get undefined

alert(easeyPeasy.inputs.length)

Besides that I can't select values in my obj via key index. It only works via key string.

working:

alert(easeyPeasy.inputs["easeIn_input"])

not working:

alert(easeyPeasy.inputs[0])

Does anyone know what I am missing here? Thanks in advance

RobC
  • 22,977
  • 20
  • 73
  • 80
Marten Zander
  • 2,385
  • 3
  • 17
  • 32

3 Answers3

3

You seem to be expecting JavaScript objects to behave like PHP arrays. They don't.

Arrays have properties whose names are numbers and a length property which is equal to the name of the highest numerical property plus one.

Objects just have named properties.

You can generate an array of property names in an object with Object.keys().

var data = {
  foo: "foo_val",
  bar: "bar_val"
};
var props = Object.keys(data);
for (var i = 0; i < props.length; i++) {
  document.body.appendChild(
    document.createTextNode(
      data[props[i]]
    )
  )
  document.body.appendChild(
    document.createElement("br")
  );
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • that helped, thank you! Btw, is it possible to define properties for properties at once? – Marten Zander Feb 23 '16 at 10:21
  • 1
    I don't understand what you mean by "properties for properties at once" – Quentin Feb 23 '16 at 10:22
  • right now I have something like: easeyPeasy.label.easeIn_label . What If I want to have something like: easeyPeasy.label.easeIn_label.sliderIndicator – Marten Zander Feb 23 '16 at 10:23
  • Are you asking something like this: `var obj = {}; obj.foo.bar = "test"`? If yes, it will not. It will throw error because `obj.foo` is not defined – Rajesh Feb 23 '16 at 10:26
  • @Rajesh yes that is what I want to achieve, almost. What If foo is defined in your case ? will it work than? – Marten Zander Feb 23 '16 at 10:36
  • @MartenZander if you want to create complex UIs you could also use resource strings. But they are hard to debug. I played a bit with them here https://github.com/fabiantheblind/extendscript/wiki/Script-UI-Resource-Strings – fabianmoronzirfas Feb 23 '16 at 10:47
0

You have created a Javascript object; it just stores values accessible via keys.

To determine the length in characters, you need to convert it to a string representation first:

var json = JSON.stringify(easeyPeasy)
Martin Zabel
  • 3,589
  • 3
  • 19
  • 34
jarst
  • 147
  • 12
  • The length will then be the number of characters in the string, not the number of items in the object. – Quentin Feb 23 '16 at 10:21
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/11373172) – Dwayne Charrington Feb 24 '16 at 00:35
  • @DigitalSea this definitely *is* an answer. Please pay more attention when reviewing, and [read this](http://meta.stackexchange.com/questions/225370) for more information. – Rob Feb 24 '16 at 04:26
0

If you want to loop over properties of an object, you can also use for-in

Example

var obj = {
  test1:"test1",
  var1:10,
  var2:20,
  var3:30
}

for (var key in obj){
  document.write(key + " :  " + obj[key] + "<br/>");
}
Rajesh
  • 24,354
  • 5
  • 48
  • 79