2

My javascript code;

function callDist(sel)
{
   $.getJSON('Dist.json', function(data){
    var $container = $('#parameters').empty();

    $.each(data.distributions, function(i, distribution){
         $.each(distribution, function(key,value) 
         {
          if(distribution.type==sel.value) {
            alert(distribution.name);
            $.each(distribution.parameters, function(key, value) {
            $container.append(key + ':' + value + '<br />');
            $container.append('<hr />');
          });
         }
    });
 }
 );
 });
 }

My Json code;

{
"distributions":[
  { 
     "name":"Uniform",
     "type":1,
     "parameters":[{ "minValue":2 , "maxValue":4 }]
  },
  {  "name":"Normal",
     "type":2,
     "parameters":[{ "mean":5 , "standartDeviation":3 }]
  },
  {
     "name":"Exponential",
     "type":3,
     "parameters":[{"lamda":2}]
  },
  {
     "name":"Geometric",
     "type":4,
     "parameters":[{"probability":0.2}]
  }
 ]
}

Html code;

<select name="selectDistribution" class="span12"  id="Options"       

 onchange="callDist(this);" >
      <option value="0">Choose one distribution</option>
      <option value="1">Uniform</option>
      <option value="2">Normal</option>
      <option value="3">Exponential</option>
      <option value="4" >Geometric</option>
</select>

In Json there is an array that created by some distributions.when the user selects some of them from select option I am triggering callDist(sel) function. When the passed value sel is equal to distribution.type , I want to write the parameters of that distribution to a div.How can I do that?Thanks.

6
  • 2
    Never call a variable JSON, as that will override the native JSON object. And what have you tried to accomplish your task? Commented Mar 21, 2013 at 15:34
  • what is sel.value supposed to be? Commented Mar 21, 2013 at 15:36
  • @MaxArt I changed it but it is still not solved. Commented Mar 21, 2013 at 15:37
  • @joeframbach It is passed from a html tag.It is an integer. Commented Mar 21, 2013 at 15:37
  • in the first each you have "," extra? Commented Mar 21, 2013 at 15:39

4 Answers 4

2
function callDist(sel) {
    $.getJSON('Dist.json', function(json){
        var $container = $('#parameters').empty();


        $.each(json.distributions, function(i, distribution) {
            if(distribution.type==sel.value) {
                $.each(distribution.parameters, function(j, parameters) {
                    $.each(parameters, function(parameter_key, parameter_value) {
                        $container.append(parameter_key + ':' + parameter_value + '<br />');
                        $container.append('<hr />');
                    });
                });
             }
        });
    });
}
Sign up to request clarification or add additional context in comments.

6 Comments

It seems true but this time, there is no output.
I run my project from wamp and localhost.And there is no error.Only div tag for paramters is empty now.
I have it running in a jsfiddle.
I see the result is correct, but why I cannot get the same result in my project:(
Have you copy/pasted my solution?
|
1

With distributions set up as an array, you'll have to loop through distributions and check to see if the type value of each element matches sel, and then append the parameters of that object to the div in question, unless position always translates to a certain type. If that's the case, something like

var distribution = data[(sel-1)]

can be used to set the distribution.

What might make more sense, since there may be cases where position in the array does not correspond to type, is to restructure your JSON so that you have an object instead of an array, like the following:

{"distributions":{
    "Uniform":{
        "name":"Uniform",
        "type":1,
         "parameters":[{ "minValue":2 , "maxValue":4 }]
    },
    "Normal":{
         "name":"Normal",
         "type":2,
         "parameters":[{ "mean":5 , "standartDeviation":3 }]
    }
  }
}

In this case, I've used the name field as the attribute names of the distributions object, and can get my parameters for any given distrubution by typing data[selName].parameters, in which selName is the name corresponding to the selection, but you could just as easily use the type as the attribute name.

Both of these methods would remove the need to loop through the data.

Comments

0

I made a fiddle around it here . I am able to see the alert with the correct value. I used

$('#Options').change()

to trigger the function and a few other modifications to work on fiddle.

1 Comment

I want to get the parameters of the wanted distribution.And I want to write the parameters to parameters div
-1

Remove the 2nd comma in

$.each(data.distributions, function(i, distribution,) {

2 Comments

I changed it but this time I got 0:[object Object] at three times.
but I can get only names of the distributions with your solution.

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.