2017-03-17 70 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