-2

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.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • 4
    Arrays will only have a name property if you give one to them. There is no inherent name property for js arrays – Ben McCormick May 07 '13 at 15:43
  • need to see a basic structure of your array of arrays. – Ryan May 07 '13 at 15:43
  • I'm guessing you mean `for(var y=0; y – PitaJ May 07 '13 at 15:44
  • @PitaJ Yes, I had some issues posting for some reason, and it didn't post what was actually in the textarea when I clicked post, it posted what I had typed a minute earlier. – undrline - Reinstate Monica May 07 '13 at 15:51
  • @ryan Something like `ActiveDocument.gaShapeArrays = new Array(gaShapesTab1, gaShapesTab2, gaShapesTab3, gaShapesTab4, gaShapesTab5);` Where gaShapesTab1 is a global array of shape objects like `ActiveDocument.gaShapesTab1 = new Array(ActiveDocument.Sections["Dashboard"].Shapes["Shape1"],ActiveDocument.Sections["Dashboard"].Shapes["Shape1"]);` – undrline - Reinstate Monica May 07 '13 at 15:57
  • @EdwardD Edit that into your question, code important to the question shouldn't be placed as a comment. – Ryan May 07 '13 at 15:59
  • You could add a new Tabx property to ActiveDocument, assign the array to it, then remove the original. But why do you want to do that? There probably is a better, cleaner way. – bfavaretto May 07 '13 at 16:20

3 Answers3

1

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?

Phil H
  • 19,928
  • 7
  • 68
  • 105
0

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
}
MMM
  • 7,221
  • 2
  • 24
  • 42
  • He could be doing something like `array = [["array_1",[1,2,3]],["array_2",[4,5,6]]]` which isn't ideal by any means but possible, and that could be what he is referring to. Again just a guess and it's not ideal. – Ryan May 07 '13 at 15:50
  • @ryan: He could, but it's not like than he can use `"array_1"` to access the array or something. – MMM May 07 '13 at 15:51
  • Ya I know, but `array_1` could be the name he is referring to, which would be one way of telling him roughly where the coordinates of the value he is looking for are. Really without more clarification from the op it's hard to see what he's really wanting to do. – Ryan May 07 '13 at 15:52
  • @ryan: We can only guess ;) – MMM May 07 '13 at 15:53
  • Simply put, I was trying find if there was any way to get at a variable's name as a string. If you have `var myvar = true;` I was wondering if there was a way to somehow get "myvar" as a string. It seems to be an overwhelming no. – undrline - Reinstate Monica Jul 25 '16 at 16:13
0

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.