I'm trying to do a for loop in javascript through every chain in an instrument rack using this:
x = new LiveAPI("id " + rID);
for (var chain in x.get("chains")) {
}
This isn't working because x.get("chains") returns "id 12 id 27 id 42"
So must I do some string processing and remove the 'id' and manually split the numbers into an array myself or is there some inbuilt method to enable me to do a for loop 'out of the box'?
enumerating chains in javascript
-
- Posts: 36
- Joined: Thu Jun 18, 2009 11:29 am
- Location: Germany
- Contact:
Re: enumerating chains in javascript
For components I use the following code. I have not tested it but I guess it works the same for chains:
var api = new LiveAPI (null, "");
var count = api.getcount ('components');
for (var j = 0; j < count; j++)
{
api.path = ['control_surfaces', i, 'components', j];
componentNames[j] = api.get ('name');
}
var api = new LiveAPI (null, "");
var count = api.getcount ('components');
for (var j = 0; j < count; j++)
{
api.path = ['control_surfaces', i, 'components', j];
componentNames[j] = api.get ('name');
}
Re: enumerating chains in javascript
Yep i do the same thing in kapture. Get the number of chains from the api with getcount, then iterate through each one.
Re: enumerating chains in javascript
Thanks for that, As I'm dealing with some deep nested chains I've tried to avoid paths and just stick to ID's using the following
for (var chainID in x.get("chains").asAPIObjects() ){
var chain = new LiveAPI("id " + chainID);
}
Array.prototype.asAPIObjects=function(){
var output = new Object()
for(var c = 0; c < this.length; c++){
if (this[c] !="id"){
output[this[c]] = new Object();
}
}
return output;
}
What I really wanted to do was have the extended function return a collection of the chains as API objects (as the method name suggests) but I couldn't get it working. Ah well the above works ok for me for now.
Thanks for tips
for (var chainID in x.get("chains").asAPIObjects() ){
var chain = new LiveAPI("id " + chainID);
}
Array.prototype.asAPIObjects=function(){
var output = new Object()
for(var c = 0; c < this.length; c++){
if (this[c] !="id"){
output[this[c]] = new Object();
}
}
return output;
}
What I really wanted to do was have the extended function return a collection of the chains as API objects (as the method name suggests) but I couldn't get it working. Ah well the above works ok for me for now.
Thanks for tips
Re: enumerating chains in javascript
Thats a great idea barstu. In python thats how it works, for example tracks returns a tuple of track objects. Would be much simpler in m4l if we could prototype each of the arrays to return the actual objects