Structure

Sorting a structure

Swiftlikeninja's picture

Okay, I have been trying to wrap my head around javascript to no avail so i call upon the assistance of the masses here at kineme. I dont know if just the structure sort patch will do what i need but in essence, i'm building a scoreboard display that will display individuals and team scores. What I would like assistance in is figuring out a way to sort a structure of based upon a substructure. If you look at the attached png as an example, i would like to be able to sort the entire structure by team name preserving the keys and structure layout.

once i figure this out my next task will be separating each team into its own structure.

If anyone could point me in the right direction I would be eternally grateful. I have tried to dig through all the javascript references on the kineme site and cyberos guide and my mind just explodes.

If I could just learn javascript i would be in heaven! :P

Structure Key at Index (Composition by smokris)

Author: smokris
License: MIT
Date: 2010.04.08
Compatibility: 10.5, 10.6
Categories:
Required plugins:
(none)

Given a Structure, returns the Key for the specified Index (instead of the Value, which "Structure Index Member" returns).

Sorting arrays and splitting arrays in Javascript

usefuldesign.au's picture

Javascript help for novice coders like me!

Apart from the fact that the JS patch is temperamental — requiring restarts of QC application when it will not parse good script — I attempted to clear up some of the mysteries I had around working with 2D datasets in Javascript. Standard JS array methods that work for 1D arrays often don't work for more complicated array structures.

This composition has three demo JS patches for:

splitting a set of points into two sets (odds and evens by index), sorting an array of dictionaries by a common dictionary key (a-Z) & concatenating both internally created and inputed structures into one array

All the code is well commented, I hope it can save some of the suffering for others that the JS patch has given me. So let me know if it does and I will have been rewarded for posting it.

add and derive average using javascript and structures

Swiftlikeninja's picture

Let me start off by saying that I am a complete noob when it comes to javascript. Id imagine that this should be easy to accomplish.

I would like to know how to take a structure and add up all the values then divide the total by the structure count providing me with an average. Currently I am accomplishing this with a rather ugly and lengthy set of structure makers to a math patch but its not dynamic, and is pretty sloppy. To go one step further I would like to be able to add up all of a certain sub index within a complete structure through javascript. Im not concerned with maintaining the original structure, the only output needed is the average value.

Im not asking anyone to make this for me but again im lost when it comes to javascript and would appreciate any assistance. Ive dugged through the post to attempt to find an answer got nowhere. Ive checked cybero's reference site but cant figure out what to combine to achieve what I need.

Passing keyed structures between JavaScript nodes

mediashowcase's picture

I'm having a heck of a time trying to figure out how to work with keyed structures that are passed between JavaScript patches.

For instance, in the first JavaScript patch, I have this...

function (__structure outputStructure) main ()
{
   var result = new Object();
   result.outputStructure = new Object();
 
 var myStructure = new Object();
    myStructure.something = new Object();
    myStructure.something.amazing = new Object();
    myStructure.something.amazing.here = "yes";
 
    result.outputStructure = myStructure;
 
 
   return result;
}

outputStructure is then connected to myStructure of JavaScript patch number two...

function (__string somethingamazing) main (__structure theStructure)
{
   var result = new Object();
 
       result.somethingamazing = theStructure.something.amazing.here;
 
   return result;
}

I expect somethingamazing to return "yes", but instead I am left with a persistent error:

TypeError:  Result of expression 'theStructure' [null] is not an object.

I originally encountered this problem when I was attempting to make use of the structure output from the MIDITools plugin, so I suppose the problem doesn't have to do specifically with passing keyed structures between JavaScript patches, but that I'm simply doing something wrong with trying to get the structure into JavaScript.

I feel, as I so often do, that I am missing something extremely obvious. Can anyone shed some light on my mistake?