简体   繁体   中英

how to use object value as object name in javascript?

how can i use the object value as object name

like this

$.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);
    });
});​

i have tried toString() it didn't work Thanks

use square-bracket notation when using variables (identifiers) instead of actual object properties

console.log(v[objectName])

For futher reference on subject : 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 [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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