简体   繁体   English

获取数组中数组的名称?

[英]Get the name of array in array?

How can I get the string name 'bar' instead of its contents ?如何获取字符串名称 'bar' 而不是其内容? Never found a solution for this one?从来没有找到解决这个问题的方法? I want console.log("?") to say 'bar'我想要 console.log("?") 说 '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" ?

As far as I know, you cannot do that.据我所知,你不能那样做。 You might need to use an object for that您可能需要为此使用一个对象

As said above, you can't do this the way it is.如上所述,你不能按照现在的方式这样做。

You could however, attach a name property to each base array.但是,您可以将 name 属性附加到每个基本数组。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM