Jitender
Jitender

Reputation: 7969

How to access object name for their value using jquery

I have on object and I want to print their name and property name. How can I do that. I can access their properties value. Like I want print object name like 'first' and 'second' and their properties like 'value' and 'text' dont want to print value

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript" src="jquery-1.7.2.js"></script>
    <script type="text/javascript">
        $(function (){
            var myDate= {
                'first':{value:'30',text:'i am the one'},
                'second':{value:'50',text:'i am the second'}
            }

            $('a').click(function (){
                var t= $(this).text();
                if(t=="both"){
                    $('.text').text(myDate['first'] + '' + myDate['second'] );
                } else {
                    $('.text').text(myDate[t]);
                }
            });
        });
    </script>
</head>
<body>
    <div class="text"></div>
    <a href="#">first</a>&nbsp;&nbsp;<a href="#">second</a>
    <a href="#">both</a>​
</body>

Upvotes: 0

Views: 1632

Answers (4)

nnnnnn
nnnnnn

Reputation: 150080

You can use a standard JS for..in loop - you don't need jQuery, though it has you covered too with its $.each() method. Either way gives you access to the property names and their corresponding values. Given you've got nested objects you will probably want nested for..in or $.each() loops.

You don't make it at all clear what format your output should be, but here's a simple example that at least shows how to get the pieces you need:

var output = "";
$.each(myDate, function(k, val) {
    // k is the property name, val is the property value
    output += k + ": ";
    $.each(val,function(k,val) {
        output += k + ": " + val + "; ";
    });
    output += "\n";
});
// do something with output

That would produce a string, output, that looks like this:

first: value: 30; text: i am the one; 
second: value: 50; text: i am the second; 

...as shown in this demo: http://jsfiddle.net/nnnnnn/WvBgD/

Upvotes: 2

Sunil Dodiya
Sunil Dodiya

Reputation: 2615

you can simply use for loop to get object name.

for(var x in myDate){
      console.log(x);
      if(typeof(myDate[x]) == "object") {
         for(var y in myDate[x]){
             console.log(">>"+y);
         }
      }
 }

RESULT......

first
>>value
>>text
second
>>value
>>text

Upvotes: 1

Tats_innit
Tats_innit

Reputation: 34117

Working demo http://jsfiddle.net/msSwA/ or http://jsfiddle.net/msSwA/1/

Good link: How to get an object's properties in JavaScript / jQuery?

Hope it fits the needs :)

value = myDate['first'].value or text = myDate['first'].text

code

$(function() {
    var myDate = {
        'first': {
            value: '30',
            text: 'i am the one'
        },
        'second': {
            value: '50',
            text: 'i am the second'
        }
    }

    $('a').click(function() {
        var t = $(this).text();
        if (t == "both") {
         $('.text').text(myDate['first'].value + ' == ' + myDate['second'].value)
        }
        else {

            $('.text').text(myDate[t].value);

        }
    })



})​

Upvotes: 0

Wern Ancheta
Wern Ancheta

Reputation: 23337

You can use for in loop:

    for(var x in myDate){
      console.log(myDate[x]['value']);//access value
      console.log(myDate[x]['text']);//access the text
    }

Upvotes: 0

Related Questions