0

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

0

1 Answer 1

4

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 [...]

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it works , can you give us more explanation of your answer
can i do something like this $.each(values, function(index,value){ for(var i=0 ; i< columns.length ; i++){console.log(value[columns[i]])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.