0

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

1

2 Answers 2

0

Try this:

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

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

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

Comments

0

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.

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.