Question

Have a good day programmers

Description : I have dropdown in my code whenever user select some category i need to search that value from json file. I done this thru change event it's working well.

$('#social_media').on('change',function(e){
        var test = this.value;
        $.ajax({
        url:"jsoncontent.json",
        dataType:"json",
        success:function(data){
            //here test variable contain 'facebook' value
            //so i gave data.test instead of data.facebook
            $.each(data.test,function(key,value){               
                    alert(value.name);
                });     
            }   
        });
    });

Problem : Here's my problem in each loop i gave data.test instead of 'data.facebook' because test is the variable which contain value 'facebook' and i know data is the object and test is the variable and sure i did mistake in handling object and variable..this is my json data

{
    "StackOverflow": [
        {
            "name": "StackOverflow",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "StackOverflow",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "StackOverflow",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "StackOverflow",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        }
    ],
    "facebook": [
        {
            "name": "facebook",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "facebook",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "facebook",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "facebook",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        }
    ],
    "twitter": [
        {
            "name": "twitter",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "twitter",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "twitter",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        },
        {
            "name": "twitter",
            "exp": "3month",
            "username": "koushik",
            "Designation": "Software Developer"
        }
    ]
} 

Thank in advance

Was it helpful?

Solution

Try this:

$.each(data[test], function (key, value) {
    alert(value.name);
});
}

If test is a variable, then we use data[test] not data.test.

OTHER TIPS

You need to create another $.each inside of the first $.each So the second $.each should take the value of 'value'

$.each(obj, (key, value) {
           $.each(obj, (keys, values) {}
           }

It's something like that, as far I think.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top