如何从jQuery中的多维数组中选择特定的索引项

时间:2014-09-06 06:22:30

标签: jquery multidimensional-array indexing

我有以下多维数组:

var practice_times = [
    ['Tuesday',['Late Afternoon (2.30-5.30pm)','Evening (5.30-8.30pm)']],
    ['Thursday',['Late Afternoon (2.30-5.30pm)','Evening (5.30-8.30pm)']],
    ['Friday',['Mid Morning (9am-12pm)','Midday (12-1pm)']]
];

我希望能够最初检索所有' days'并放入select元素,然后检索所有' times'并将其放在不同的选择元素中。

非常感谢任何帮助 - 在这个问题上抓了几个小时。

6 个答案:

答案 0 :(得分:0)

根据您的要求创建2个数组并填充数组。

var days = [],times= []
$.each(practice_times,function(index,value){
    days.push(value[0]);
    var objTime = value[1];
    times.push(objTime[0]);
    times.push(objTime[1]);
});

希望这会有所帮助:)

答案 1 :(得分:0)

var practice_times = [
    ['Tuesday', ['Late Afternoon (2.30-5.30pm)', 'Evening (5.30-8.30pm)']],
    ['Thursday', ['Late Afternoon (2.30-5.30pm)', 'Evening (5.30-8.30pm)']],
    ['Friday', ['Mid Morning (9am-12pm)', 'Midday (12-1pm)']]
];

//console.log(practice_times);

var day = [];
var Time = [];
for (var i in practice_times) {

    day.push(practice_times[i][0]);
    Time.push(practice_times[i][1]);
}
$("div:first").text(day);
$("div:eq(1)").text(Time);

DEMO

答案 2 :(得分:0)

你可以试试这个

<select class="day"></select>
<select class="time"></select>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script>
var practice_times = [
    ['Tuesday',['Late Afternoon (2.30-5.30pm)','Evening (5.30-8.30pm)']],
    ['Thursday',['Late Afternoon (2.30-5.30pm)','Evening (5.30-8.30pm)']],
    ['Friday',['Mid Morning (9am-12pm)','Midday (12-1pm)']]
];
var day, time;
for(var i = 0; i < practice_times.length; i++){
    day = '<option value="'+practice_times[i][0]+'">'+practice_times[i][0]+'</option>';
    $('.day').append(day);
    time = '<option value="'+practice_times[i][1][0]+'">'+practice_times[i][1][0]+'</option>' +
        '<option value="'+practice_times[i][1][1]+'">'+practice_times[i][1][1]+'</option>';
    $('.time').append(time);
}
</script>

答案 3 :(得分:0)

你应该这样试试

var text= new Object();
    text["a"] = "one";
    text["b"] = "two";

    for (key in text){
         console.log(text[key]);
         console.log(key);
    }

使用此功能,您将获得密钥及其价值,并且易于管理。

答案 4 :(得分:0)

尝试使用此选项从阵列中创建选择选项:

 <select id='days'></select>
 <select id='time1'></select>
 <select id='time2'></select>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script>
    $(document).ready(function(){
    var practice_times = [
    ['Tuesday',['Late Afternoon (2.30-5.30pm)','Evening (5.30-8.30pm)']],
    ['Thursday',['Late Afternoon (2.30-5.30pm)','Evening (5.30-8.30pm)']],
    ['Friday',['Mid Morning (9am-12pm)','Midday (12-1pm)']]
   ];
   var i,j;
   var days,time1,time2='';  

   $.each(practice_times, function(key1, value1){

        var str = String(value1).split(","); 
         days += "<option>"+str[0] + "</option>";
         time1 += "<option>"+str[1]+ "</option>";
         time2 += "<option>"+str[2]+ "</option>";

   });
  $('#days').html(days);
  $('#time1').html(time1);
  $('#time2').html(time2);

});    

答案 5 :(得分:0)

感谢大家的帮助。我忘了提到我有多天(3)套时间来处理。这是我提出的解决方案,由您的一些输入提示:

/* Contact Form Setup */
var practice_times = [

  [
    ['Tuesday',['Late Afternoon (2.30-5.30pm)','Evening (5.30-8.30pm)']],
    ['Thursday',['Late Afternoon (2.30-5.30pm)','Evening (5.30-8.30pm)']],
    ['Friday',['Mid Morning (9am-12pm)','Midday (12-1pm)']]
  ],
  [
    ['Tuesday',['Early Afternoon (1-2.30pm)','Late Afternoon (2.30-5.30pm)']]
  ],
  [
    ['Monday',['Mid-morning (9am-12pm)','Midday (12-1pm)','Early Afternoon (1-2.30pm)','Late Afternoon (2.30-5.30pm)']],
    ['Tuesday',['Mid-morning (9am-12pm)','Midday (12-1pm)','Early Afternoon (1-2.30pm)','Late Afternoon (2.30-5.30pm)']],
    ['Wednesday',['Mid-morning (9am-12pm)','Midday (12-1pm)','Early Afternoon (1-2.30pm)','Late Afternoon (2.30-5.30pm)']],
    ['Thursday',['Mid-morning (9am-12pm)','Midday (12-1pm)','Early Afternoon (1-2.30pm)','Late Afternoon (2.30-5.30pm)']],
    ['Friday',['Mid-morning (9am-12pm)','Midday (12-1pm)','Early Afternoon (1-2.30pm)','Late Afternoon (2.30-5.30pm)']]
  ]                     
];

var getDays = function(index) {
    practiceIndex = index;
    days = [];
    for(var i in practice_times[index]) {
        days.push(practice_times[index][i][0]);
    }
    return days;
}

var getTimes = function(index) {
    times = practice_times[practiceIndex][index][1];
    return times;
}

var updateSelect = function(selectName,optArray) {
    //init options
    var selectOptions = "<option value='-'>- Preferred " + selectName.charAt(0).toUpperCase() + selectName.substring(1) + " -</option>";
    //populate options
    for(var i=0; i < optArray.length; i++) {
        console.log(optArray[i][0]);
        selectOptions+= "<option>" + optArray[i] + "</option>";
    }
    //add options to select and show
    $('.contact-form select[name=' + selectName + ']').empty().append(selectOptions).show();
}

var toggleSelect = function(selectToToggle,selectVal) {
    selectToToggle.toggle(selectVal == '-' ? false : true);
}

//Initialise
var practiceIndex = -1;
$('.contact-form .day-time').hide();
$('.contact-form select[name=time]').hide();
//Select change events
$('.contact-form select[name=practice]').on('change',function(){ updateSelect('day',getDays($(this).prop('selectedIndex')-1)); toggleSelect($('.contact-form .day-time'),$(this).val()); });
$('.contact-form select[name=day]').on('change',function(){ updateSelect('time',getTimes($(this).prop('selectedIndex')-1)); toggleSelect($('.contact-form select[name=time]'),$(this).val()); });