0

I have this array structure :

var $obj = {
        'sections1' : {
            'row1' : {
                'key1' : 'input1',
                'key2' : 'inpu2'
            },
            'row2' : {
                'key1' : 'input1',
                'key2' : 'inpu2'
            }
        },
        'sections2' : {
            'row3' : {
                'key1' : 'input1',
                'key2' : 'inpu2'
            },
            'row4' : {
                'key1' : 'input1',
                'key2' : 'inpu2'
            }
        }
    };

I want to fetch data from $obj->section2->row4->key2. How can I make it using jquery and javascript both.

UPDATE: I want it to print the value in a paragraph or div

<p id="array"></p>
8
  • 2
    $obj.section2.row4.key2 just replace -> with dots. Commented Jun 8, 2017 at 11:57
  • var Data=$obj.section2.row4.key2; Commented Jun 8, 2017 at 11:58
  • @NinaScholz and one more thing I want to print it in a div or para Commented Jun 8, 2017 at 11:59
  • Also this is not an array structure, but an java script object! Commented Jun 8, 2017 at 11:59
  • should the access be dynamic? Commented Jun 8, 2017 at 11:59

3 Answers 3

1

There are different ways. The best suited would be:

  1. Bracket Notation

    var prop = object['property_name'];

  2. Dot Notation

    var prop = object.property_name;

In your case it would be

var obj = {
        'sections1' : {
            'row1' : {
                'key1' : 'input1',
                'key2' : 'inpu2'
            },
            'row2' : {
                'key1' : 'input1',
                'key2' : 'inpu2'
            }
        },
        'sections2' : {
            'row3' : {
                'key1' : 'input1',
                'key2' : 'inpu2'
            },
            'row4' : {
                'key1' : 'input1',
                'key2' : 'inpu2'
            }
        }
    };
    
    
    console.log(obj.sections2.row4.key2);//Dot notation
    console.log(obj['sections2']['row4']['key2']);//Bracket notation
    document.getElementById("array").innerHTML=obj.sections2.row4.key2;
<p id="array"></p>

For more details refer MDN Property Accessors

Hoe it helps :)

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

Comments

1

In javascript use . (dot) notation to access values

var obj = {
        'sections1' : {
            'row1' : {
                'key1' : 'input1',
                'key2' : 'inpu2'
            },
            'row2' : {
                'key1' : 'input1',
                'key2' : 'inpu2'
            }
        },
        'sections2' : {
            'row3' : {
                'key1' : 'input1',
                'key2' : 'inpu2'
            },
            'row4' : {
                'key1' : 'input1',
                'key2' : 'inpu2'
            }
        }
    };
    
    
    console.log(obj.sections2.row4.key2);
    
    

1 Comment

Thanks you so much for your answer
1

Actually it looks quiet simple your requirement You can use the array notation or (.) dot operator

1) Array notation $obj['section2']['row4']['key2'];

2) Dot operator $obj.section2.row4.key2;

Nb: Please use array notation It would be safe in case of unknown keys if the key has a space in it

If you need it iterate through all the keys you can use for in loop

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.