2017-03-17 67 views
0
對象數組

我的數據是這樣,混亂的循環使用jQuery

var menuItems = { 
    "titles":[ 
     { 
     "title_id":"Chief Information Officer (CIO)", 
     "title_name":"Chief Information Officer (CIO)" 
     }, 
     { 
     "_title_id":"Chief Technology Officer (CTO)", 
     "title_name":"Chief Technology Officer (CTO)" 
     } 
    ], 
    "skills":[ 
     { 
     "skill_id":1000185, 
     "skill_name":"ITSoftware Development" 
     }, 
     { 
     "skill_id":1000186, 
     "skill_name":"Network Security" 
     } 
    ] 
}; 

我想遍歷所有數據和附加價值來選擇框選項的值和名稱。因此,對於測試我使用jQuery的下面,

$.each(menuItems, function (key, value) { 
     { 
      if (key == "titles") { 
       $.each(value, function (key1, value1) { 

        for(k in value1) { 
         $('.title-append').append($('<option>', { 
               value: value1[k], 
               text: value1[k] 
         })); 

        } 
       }) 
      } 
     } 
     }); 

HTML代碼:

<select name="position" class="form-control title-append" id="position"><option value="">Select..</option></select> 

所有的值都在選擇框中追加兩次。我被困在這裏,我嘗試了不同的情況,但似乎沒有任何工作。 jQuery有什麼問題嗎?

enter image description here

+0

這裏沒有JSON,只是數組和對象。 JSON只是一個字符串/文本格式。在你做'$ .each'的時候,它已經被解析了。 –

回答

0

由於您只處理標題,你應該只迭代menuItems.titles而不是整個menuItem對象:

menuItems.titles.forEach(function(title) { 
    $('.title-append').append($('<option>', { 
     value: title.title_id, 
     text: title.title_name, 
    })); 
}); 
0

只要簡化您的疊代的JSON,

你遍歷特定的鍵「標題」,通過提供它作爲你想迭代它的數組直接遍歷它。

$.each(menuItems['titles'], function(key, value) { 
    $('.title-append').append($('<option>', {value: value['title_id'],text: value1['title_name']})); 
}); 
-2

你不需要有內部的循環,因爲你的值1對象已經將有2對象和你的每個將循環它4,嘗試這種方式..


 

 
$(function(){ 
 
    var menuItems = { 
 
     "titles":[ 
 
      { 
 
      "title_id":"Chief Information Officer (CIO)", 
 
      "title_name":"Chief Information Officer (CIO)" 
 
      }, 
 
      { 
 
      "title_id":"Chief Technology Officer (CTO)", 
 
      "title_name":"Chief Technology Officer (CTO)" 
 
      } 
 
     ], 
 
     "skills":[ 
 
      { 
 
      "skill_id":1000185, 
 
      "skill_name":"ITSoftware Development" 
 
      }, 
 
      { 
 
      "skill_id":1000186, 
 
      "skill_name":"Network Security" 
 
      } 
 
     ] 
 
    }; 
 

 
    $.each(menuItems, function (key, value) { 
 
      { 
 
       if (key == "titles") { 
 
        $.each(value, function (key1, value1) { 
 
          $('.title-append').append($('<option>', { 
 
                value: value1.title_id, 
 
                text: value1.title_name 
 
          })); 
 
        }) 
 
       } 
 
if (key == "skills") { 
 
        $.each(value, function (key1, value1) { 
 
          $('.student-append').append($('<option>', { 
 
                value: value1.skill_Id, 
 
                text: value1.skill_name 
 
          })); 
 
        }) 
 
       } 
 
      } 
 
      }); 
 
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select name="position" class="form-control title-append" id="position"><option value="">Select..</option></select> 
 
<select name="Students" class="form-control student-append" id="Students"><option value="">Select..</option></select>

1

沒有必要的jQuery:

var menuItems = { 
 
    "titles": [{ 
 
     "title_id": "Chief Information Officer (CIO)", 
 
     "title_name": "Chief Information Officer (CIO)" 
 
    }, 
 
    { 
 
     "title_id": "Chief Technology Officer (CTO)", // <-- you had a typo in this key 
 
     "title_name": "Chief Technology Officer (CTO)" 
 
    } 
 
    ], 
 
    "skills": [{ 
 
     "skill_id": 1000185, 
 
     "skill_name": "ITSoftware Development" 
 
    }, 
 
    { 
 
     "skill_id": 1000186, 
 
     "skill_name": "Network Security" 
 
    } 
 
    ] 
 
}; 
 

 
// retrieve the select 
 
var dropdown = document.getElementById('position'); 
 

 
// iterate over array 
 
menuItems['titles'].forEach(obj => { 
 
    var option = document.createElement('option'); // create a new option 
 
     option.value = obj.title_id;    // set the value attribute 
 
     option.textContent = obj.title_name;  // set what's displayed 
 
    dropdown.appendChild(option);     // attach to document 
 
});
<select name="position" class="form-control title-append" id="position"> 
 
    <option value="">Select..</option> 
 
</select>

+0

當prob有簡單的解決方案時,爲什麼有人想改變他的結構...? – user7417866

+0

@ user7417866也許您因爲我正在使用新帳戶而投票給我?如果是這樣,你被誤導了。 **(1)** IE支持大量的ES6(畢竟它只是ES5的擴展,而不是一種全新的語言);微軟Edge幾乎可以完全支持它**(2)**沒有結構改變VanillaJS效率更高,就像jQuery一樣簡單(正如我已經證明的那樣) – 2017-03-17 18:26:22

+0

不是因爲你是新的,而是因爲你建議在不需要的時候改變問題的結構,簡單的調整就可以解決問題。並不是所有的用戶都有Edge,它應該最低支持IE 10作爲標準(如果是這樣,我對ES6評論道歉,我已經刪除了它) – user7417866