|
Transform Indexed Structure into Keyed Structure, via JavascriptHi, title is pretty self explanatory: i'm trying to convert an indexed structure into a named key structure, via javascript. But so far it is not working .... Would anyone have the kindness to give me a tiny push... thxx here's the code: function (__structure structureOut) main (__number count, __structure structureIn) { var structureOut = new Object(); for(i = 0; i < count; i++) { structureOut.i = structureIn[i] ; } return structureOut; } |
You need a result object to store the structure. Also, using structureOut.i will always assign just a key "i" -- I think what I have here will give you other keys, but I didn't really check. You also don't necessarily need a count input (unless you want to work with just a subset of the structure) -- the number of elements can be found by using structureIn.length (or something like that).
Thanks Frantz and Chris, i modify it a little bit and it is now just the code for my actual need : Extract a particular "second level"member in a structure of structure and output it as a new one.
thanks Chris. It is now working as a charm .... !
oops... result.structureOut must be a new Array() (not a new Object () )to preserve indexes orders ;)
to my experience, it is a better bet to use Named index instead , as indexes number can get messed up very easily. As an example, KnM Structure Writer doesn't preserve indexes numbers...
ok frantz, do you want to say that i can force indexes to be named (with index number) with :