0

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>
0

4 Answers 4

2

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/

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

Comments

1

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

Comments

0

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
    }

1 Comment

I dont want to access their value I just want to print their property name like if I click on first then 'value' and 'text' should be print not their contaning value
0

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);

        }
    })



})​

Comments

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.