简体   繁体   English

JSON使用命名索引访问数组对象

[英]JSON accessing array objects using a named index

Suppose I have this HTML 假设我有这个HTML

<form action="#" action="get">

    <select name="college" id="college" onchange="selection('college', 'course')">
        <option value="">Select an Option</option>
        <option value="amity">Amity University</option>
        <option value="indraprastha">Indraprasth University</option>
    </select>

    <br>
    <select name="course" id="course" onchange="selection('course', 'stream')" >
        <option value="">Select an Option</option>
    </select>

    <br>
    <select name="stream" id="stream">
        <option value="">Select an Option</option>
    </select>

</form>

I have this JSON, 我有这个JSON,

{
  "amity":{
    "course":[
      {
        "name":"B.Tech",
        "value":"btech",
        "stream":[
          {
            "name":"Computer Science",
            "value":"cse"
          },
          {
            "name":"Information Technology",
            "value":"cse"
          },
          {
            "name":"Aerospace Engg.",
            "value":"cse"
          }
        ]
      },
      {
        "name":"M.Tech",
        "value":"mtech",
        "stream":[
          {
            "name":"Networking",
            "value":"net"
          },
          {
            "name":"telecommunications",
            "value":"tc"
          }
        ]
      }
    ]
  }
}

The Javascript is, Javascript是

function selection(s1, s2) {
    var first = document.getElementById(s1),
        second = document.getElementById(s2);

    var college = $('#college').val(),              
        cr = $('#course').val(),
        st = $('#stream').val(),
        se = $('#sem').val();

    $.getJSON("json/select.json", function(data) {

    switch(s1) {        
        case 'college':
            $.each(data[college].course, function(key, value) {
                second.innerHTML += '<option value="'+ value.value +'">'+ value.name +'</option>';
            }); break;

        case 'course':
            $.each(data[college].course[].stream, function(key, value) {
                second.innerHTML += '<option value="'+ value.value +'">'+ value.name +'</option>';
            }); break;  
    }
});
}

I am making a dynamic drop-down menu where the next drop down values are fetched from JSON object file, using the reference of previous values. 我正在制作一个动态下拉菜单,其中使用先前值的引用从JSON对象文件中提取下一个下拉值。 As suggested from my this question ( link ), I am able to get the value of course (second drop-down) using the course array in the object. 正如我从这个问题( 链接 )中所建议的那样,我能够使用对象中的课程数组来获得课程的价值(第二个下拉列表)。

Now, since the values in the second select menu(course) are filled dynamically, I can't figure out how to take the corresponding course array element to fill the next select menu options for stream array. 现在,由于第二个选择菜单(课程)中的值是动态填充的,所以我不知道如何采用相应的课程数组元素来填充流数组的下一个选择菜单选项。

Since the course property in JSON is an array, I don't know which index element element is chosen from second menu (See the switch case for 'course', the data[college].course[] index is empty). 由于JSON中的course属性是一个数组,因此我不知道从第二个菜单中选择了哪个索引元素元素(请参阅“课程”的开关案例, data[college].course[]索引为空)。 The hardcoded [0] works, but that's not dynamic then. 硬编码的[0]可以工作,但是那不是动态的。

How to access the stream array using the values of course grabbed from second menu. 如何使用从第二个菜单中获取的课程值访问stream数组。

I hope I am clear. 我希望我清楚。 Thanks in advance! 提前致谢!

Just iterate through array of courses to get the stream dynamically: 只需遍历一系列课程即可动态获取流:

for (var i = 0; i < data[college].course.length; i++) { 
    currentStream = data[college].course[i].stream; 
}

Ie using your code: 即使用您的代码:

for (var i = 0; i < data[college].course.length; i++) {
    $.each(data[college].course[i].stream, function(key, value) {
        second.innerHTML += '<option value="'+ value.value +'">'+ value.name +'</option>';
    });
}

Finding the current stream for your selected course: 查找所选课程的当前流:

// assuming cr = "btech"
for (var i = 0; i < data[college].course.length; i++) {
    if (data[college].course[i].value == cr) {
        currentStream = data[college].course[i].stream;
        break;
    }
}

$.each(currentStream, function(key, value) {
    second.innerHTML += '<option value="'+ value.value +'">'+ value.name +'</option>';
});
function selection(s1, s2) {
var first = document.getElementById(s1),
    second = document.getElementById(s2);

var college = $('#college').val(),              
    cr = $('#course').val(),
    st = $('#stream').val(),
    se = $('#sem').val();

$.getJSON("json/select.json", function(data) {

switch(s1) {        
    case 'college':
        $.each(data[college].course, function(key, value) {
            second.innerHTML += '<option value="'+ value.value +'">'+ value.name +'</option>';
        }); break;

    case 'course':
        var course = data[college].course;
        for(var i = 0;i<course.length;i++){
            if(course[i].name === cr){ //cr is selected option                    
                $.each(course[i].stream, function(key, value) {
            second.innerHTML += '<option value="'+ value.value +'">'+ value.name +'</option>';
            }
        }

        }); break;  
}

}); }); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM