Transform Indexed Structure into Keyed Structure, via Javascript

franz's picture

Hi, 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;
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

cwright's picture
Re: Transform Indexed Structure into Keyed Structure, via ...

function (__structure structureOut) main (__number count, __structure structureIn)
{
   var result = new Object();
   result.structureOut = new Object();
   for(i = 0; i < count; i++)
   {
      result.structureOut[""+i] = structureIn[i] ;
   }
 
   return result;
}

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).

yanomano's picture
Re: Transform Indexed Structure into Keyed Structure, via ...

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.

function (__structure structureOut) main ( __structure structureIn,__string MemberToExtract)
{
  var result = new Object();
  var MTE=MemberToExtract;
 
  if (!_testMode) { 
 
    var count= structureIn.length;
 
    result.structureOut = new Object();
    for(i = 0; i <count; i++)
    {
        result.structureOut[i] = structureIn[i][MTE] ;
    }
             }
    return result;
}

franz's picture
Re: Transform Indexed Structure into Keyed Structure, via ...

thanks Chris. It is now working as a charm .... !

yanomano's picture
Re: Transform Indexed Structure into Keyed Structure, via ...

oops... result.structureOut must be a new Array() (not a new Object () )to preserve indexes orders ;)

function (__structure structureOut) main ( __structure structureIn,__string MemberToExtract)
{
  var result = new Object();
  var MTE=MemberToExtract;
 
  if (!_testMode) { 
 
    var count= structureIn.length;
 
    result.structureOut = new Array();
    for(i = 0; i <count; i++)
    {
   result.structureOut[i] = structureIn[i][MTE];
 
    }
             }
    return result;
}

franz's picture
Re: Transform Indexed Structure into Keyed Structure, via ...

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

yanomano's picture
Re: Transform Indexed Structure into Keyed Structure, via ...

ok frantz, do you want to say that i can force indexes to be named (with index number) with :

result.structureOut[""+i] 
 
/* instead of */
result.structureOut[i]