Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object iterator #138

Open
mykael22000 opened this issue Mar 13, 2014 · 4 comments
Open

Object iterator #138

mykael22000 opened this issue Mar 13, 2014 · 4 comments

Comments

@mykael22000
Copy link

Can we get a simple loop mechanism to iterate over the properties of a javascript object?

<<for $ix in $jvar>>
  <<print $jvar[$ix]>>
<<endfor>

This would make it a lot easier and more efficient to process arrays and dictionaries.

If $jvar isn't a java script object, there should be an error message.
If $jvar is a proper array, $ix should run over just the valid numerical keys (0 to $jvar.length - 1)
Otherwise $ix run through the complete list of properties defined for the object.

@greyelf
Copy link
Contributor

greyelf commented Mar 18, 2014

The format of the 'for' example you have suggested normally would have $ix equal to the current array element's 'value' not it's 'index'.

But otherwise a good idea.

@mykael22000
Copy link
Author

It's important to get the key - think this is what you are saying. For arrays it doesn't matter so much, but for non-arrays the data in the key may be significant and not present in the data object.

@greyelf
Copy link
Contributor

greyelf commented Mar 19, 2014

Plain arrays are indexed using an integer. eg $list[2]
Associative arrays use object based keys, normally strings. eg. $list['name']

If you want to loop through the keys of an Associative array I would suggest:

for $key in Object.keys($list)

@mykael22000
Copy link
Author

OK, that would be a good way of doing it. Just have to remember to put a copy in the wiki if it gets implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants