gpt4 book ai didi

javascript - 如何在javascript中使用对象值作为对象名称?

转载 作者:行者123 更新时间:2023-11-28 16:11:54 24 4
gpt4 key购买 nike

如何使用对象值作为对象名称

像这样

$.each(columns, function(index, value) {
var objectName = value.object; // need to pass this
to the next $.each
//lets say i'm expecting objectName = x
$.each(values, function(i, v) {
// console.log(v.x) ; // this works fine
console.log(v.objectName);
});
});​

我尝试过toString()但没有成功谢谢

最佳答案

使用变量(标识符)而不是实际对象属性时使用方括号表示法

console.log(v[objectName])
<小时/>

有关主题的进一步引用:http://www.jibbering.com/faq/faq_notes/square_brackets.html

String Variables as Identifiers
The string used within the square brackets does not need to be a string literal. It can be any expression that results in a string. This is where the square bracket notation becomes most useful as the expression could be a reference to a string variable that holds the property name as its value, or a function call that returns a string, or an expression that concatenates two strings, or any combination of these [...]

关于javascript - 如何在javascript中使用对象值作为对象名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12674003/

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