Mamba76
Mamba76

Reputation: 39

Get the name of array in array?

How can I get the string name 'bar' instead of its contents ? Never found a solution for this one? I want console.log("?") to say 'bar'

let foo = [1,2,3], bar = [11,22,33], hello = [111,222,333]
    let arr = [foo, bar, hello]
    console.log(arr[1])
    // returns 11, 22, 33  ** I know this
    // I want it to return "bar" ?

Upvotes: 0

Views: 178

Answers (2)

WakeskaterX
WakeskaterX

Reputation: 1428

As said above, you can't do this the way it is.

You could however, attach a name property to each base array.

For example:

const arr1 = [1,2,3], arr2 = [2,3,4];
arr1.name = "arr1";
arr2.name = "arr2";

const arrBoth = [arr1, arr2];

console.log(arrBoth[0].name);

Upvotes: 1

gilles_yvetot
gilles_yvetot

Reputation: 34

As far as I know, you cannot do that. You might need to use an object for that

Upvotes: 1

Related Questions