Question

Below, I have an array of arrays of objects. I go through looking for my object, and once I find which array it's in, I want to get at and work with that array's name as a string. My guess, was something like Array.name (as it plays out below), but that doesn't work.

ActiveDocument.gaShapesTab1 = new Array(ActiveDocument.Sections["Dashboard"].Shapes["Shape1"],ActiveDocument.Secti‌​ons["Dashboard"].Shapes["Shape2"]); 
ActiveDocument.gaShapesTab2 = new Array(ActiveDocument.Sections["Dashboard"].Shapes["Shape3"],ActiveDocument.Secti‌​ons["Dashboard"].Shapes["Shape4"]); 
ActiveDocument.gaShapesTab3 = new Array(ActiveDocument.Sections["Dashboard"].Shapes["Shape5"],ActiveDocument.Secti‌​ons["Dashboard"].Shapes["Shape6"]); 
ActiveDocument.gaShapeArrays = new Array(gaShapesTab1, gaShapesTab2, gaShapesTab3);

// go through an array of arrays
for(var x=0; x<gaShapeArrays.length; x++)
{ 
    // and go through the objects of each one
    for(var y=0; y<gaShapeArrays[x].length; y++)
    {
        // if "object" is in the array
        if(object == gaShapeArrays[x][y])
        {
            // get "sidetab" from object's array's name
            var sidetab = gaShapeArrays[x].name.replace('gaShapes',''); // assumes that shapearrays will have naming convention gaShapesSidetab
            // we found it, we can stop now
            break;
        }
    }
}

I'm working in Hyperion Intelligence, so not all Javascript will apply. For instance I don't have access to window or document.

Each array contains a set of shape objects related to a visual tab. This allows me to show or hide or do more complex operation with what's on each tab simply by calling the array of shapes. But, when working with the shapes, themselves, I need to know which tab they're on. I'm trying to work backwards by finding which array they're in.

Was it helpful?

Solution

You don't want to do that.

If you really need to find a value in several arrays and then pull out an identifier, then you want a dictionary, not named variables:

var dictOfArrays = {
    'evens': [0,2,4,6,8,10],
    'odds': [1,3,5,7,9]
};

This stores the identifier that you seek as data, so you can store that identifier and use it later to retrieve the value if you want:

var whichArrayKey = findMyValuesKey(value, dictOfArrays);
console.log('Value '+value+' is in array keyed '+whichArrayKey);
var matchingArray = dictOfArrays[whichArrayKey];
var firstValueInMatchingArray = matchingArray[0];

The name of a variable is just something for you, the developer, to use to know which thing is which. It's just a handle for a place in memory where stuff is stored. As such, it doesn't mean anything to the code. If you actually want to use it in the program, then it is data, not code, and should be encoded in a data structure like a dictionary as above. That way you can pass the array or the identifier around as much as you please, and the behaviour of the code doesn't have to be tied to the names you give your variables.

Edit 1:

The newly added code, in dictionary form/object notation:

ActiveDocument.gaShapeArrays = {
    'gaShapesTab1' : [ 
        ActiveDocument.Sections["Dashboard"].Shapes["Shape1"], 
        ActiveDocument.Secti‌​ons["Dashboard"].Shapes["Shape2"]
    ], 
    'gaShapesTab2' : [
        ActiveDocument.Sections["Dashboard"].Shapes["Shape3"],
        ActiveDocument.Secti‌​ons["Dashboard"].Shapes["Shape4"]
    ],
    'gaShapesTab3' : [
        ActiveDocument.Sections["Dashboard"].Shapes["Shape5"],
        ActiveDocument.Secti‌​ons["Dashboard"].Shapes["Shape6"]
    ]
}

So each key (e.g. 'gaShapesTab1') is paired with an array value ([...]). This is instead of using new Array() everywhere.

Once you have found the key of the array containing a reference matching your object, you'll have that key as a string (e.g. "gaShapesTab3"). You can't change this string in-place, and I don't think you'd want to. If you could clarify why you need to change the name of the array, perhaps it will be clear how to resolve the problem. For example, do you have other code that needs the array to have a particular name?

OTHER TIPS

Array's name? Arrays do not have names. You only have variable names, variables that store your arrays. If you have a two-dimensional array, you need to grab the "coordinates".

So:

if(object == gaShapeArrays[x][y])
{
    // Found the object! It's in [x][y], so in array gaShapeArrays[x] which
    // itself is in gaShapeArrays
}

Even though I think @Phil H gave me the answer to my question, as the proper way to do it, I have other reasons to do it the way @ben336 was commenting. It might not be proper, but I'm posting what the solution was in the end. Fortunately, I already had the gaSidetabs array elsewhere in my startup script for another function. I just assigned a string value to the .name property of each array. Would've been nice to know if there was a way to "get at" the symbolic name (or whatever you want to call it) that I called the array, but it sounds like that's just not possible.

ActiveDocument.gaShapesTab1 = new Array(ActiveDocument.Sections["Dashboard"].Shapes["Shape1"],ActiveDocument.Sections["Dashboard"].Shapes["Shape2"]);
ActiveDocument.gaShapesTab2 = new Array(ActiveDocument.Sections["Dashboard"].Shapes["Shape3"],ActiveDocument.Sections["Dashboard"].Shapes["Shape4"]);
ActiveDocument.gaShapesTab3 = new Array(ActiveDocument.Sections["Dashboard"].Shapes["Shape5"],ActiveDocument.Sections["Dashboard"].Shapes["Shape6"]);
ActiveDocument.gaShapeArrays = new Array(gaShapesTab1, gaShapesTab2, gaShapesTab3);
ActiveDocument.gaSidetabs = new Array('Tab1','Tab2','Tab3');

// Assigns a .name javascript property to each array.  assumes the order and length of the arrays is the same.
if (gaShapeArrays.length == gaSidetabs.length) 
    {
        for (var x = 0; x < gaShapeArrays.length; x++) 
        {
            gaShapeArrays[x].name = gaSidetabs[x];
        }
    }
else 
{
    Console.Writeln('Warning: gaShapeArrays and gaSidetabs are not the same length. Some names will not be assigned.');
}

// go through an array of arrays
for(var x=0; x<gaShapeArrays.length; x++)
{ 
    // and go through the objects of each one
    for(var y=0; y<gaShapeArrays[x].length; y++)
    {
        // if "object" is in the array
        if(object == gaShapeArrays[x][y])
        {
            // get "sidetab" from object's array's name
            var sidetab = gaShapeArrays[x].name.replace('gaShapes',''); // assumes that shapearrays will have naming convention gaShapesSidetab
            // we found it, we can stop now
            break;
        }
    }
}

Alert(sidetab);

Also glad I could figure out how to retain the format of the code block, here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top