gpt4 book ai didi

javascript - 使用字符串访问JS数组对象

转载 作者:行者123 更新时间:2023-12-01 02:01:56 25 4
gpt4 key购买 nike

var arr ={count:12,total:110};
console.log(arr.count,'',arr.total) //12 110

但是如何这样使用

var c='count';
console.log(arr.c) //it's not similar to arr.count is there is any way to use this

如何通过引用字符串来访问数组对象?

最佳答案

var c='count';
console.log(arr[c])

您可以使用括号表示法访问键/属性

关于javascript - 使用字符串访问JS数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50482863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com