我有一个JSON对象,它的stringify就像小提琴一样。JSON对象到jquery select array
http://jsfiddle.net/8TT4p/2289/
我想将其解析到键值阵列,这样我就可以在选择框中存储。
下面是我期望有
var convertedArray = {
'VAS_1000' : 'AMC',
'VAS_1001' : 'EW',
'VAS_1002' : 'EW',
}
我有一个JSON对象,它的stringify就像小提琴一样。JSON对象到jquery select array
http://jsfiddle.net/8TT4p/2289/
我想将其解析到键值阵列,这样我就可以在选择框中存储。
下面是我期望有
var convertedArray = {
'VAS_1000' : 'AMC',
'VAS_1001' : 'EW',
'VAS_1002' : 'EW',
}
可以使用Array.prototype.map()
功能。
var o = {"tuple":[{"old":{"MST_VAS_TYPE":{"MST_VAS_TYPE_ID":"VAS_1000","VAS_TYPE_NAME":"AMC","VAS_TYPE_DESC":"Annual Maintenance Contract","CREATED_ON":null,"CREATED_BY":null,"MODIFIED_ON":null,"MODIFIED_BY":null,"@xmlns":"http://services.vw.com/lpms/1.0/wsapp"}},"@xmlns":"http://services.vw.com/lpms/1.0/wsapp"},{"old":{"MST_VAS_TYPE":{"MST_VAS_TYPE_ID":"VAS_1001","VAS_TYPE_NAME":"EW","VAS_TYPE_DESC":"Extended Warranty","CREATED_ON":null,"CREATED_BY":null,"MODIFIED_ON":null,"MODIFIED_BY":null,"@xmlns":"http://services.vw.com/lpms/1.0/wsapp"}},"@xmlns":"http://services.vw.com/lpms/1.0/wsapp"},{"old":{"MST_VAS_TYPE":{"MST_VAS_TYPE_ID":"VAS_1002","VAS_TYPE_NAME":"COUPON","VAS_TYPE_DESC":"Recall","CREATED_ON":null,"CREATED_BY":null,"MODIFIED_ON":null,"MODIFIED_BY":null,"@xmlns":"http://services.vw.com/lpms/1.0/wsapp"}},"@xmlns":"http://services.vw.com/lpms/1.0/wsapp"}],"@xmlns:SOAP":"http://schemas.xmlsoap.org/soap/envelope/","@xmlns":"http://services.vw.com/lpms/1.0/wsapp"};
a= o.tuple.map(function(val){
var inner = val['old']['MST_VAS_TYPE'];
var ret = {};
ret[inner["MST_VAS_TYPE_ID"]] = inner['VAS_TYPE_NAME'];
return ret;
})
sel = document.createElement("select");
document.body.appendChild(sel);
for (var index in a) {
obj = a[index];
for (var prop in obj){
option = document.createElement("option");
option.text = obj[prop];
option.value = prop;
sel.appendChild(option);
}
}
//console.log(a);
当绑定到选择框时,该值就像0,1,2。这里是fiddle.http://jsfiddle.net/8TT4p/2293/ – user1650864
@ user1650864我更新了答案。你可以检查一下吗? – RRK
只是为了例如我把字符串div来很容易地得到它变成剧本。
我正在使用eval
将其转换为JavaScript对象。
在您必须反对之后,您可以使用for -> in
迭代其属性,并为每个option
标记生成html。
最后,将html放入select
标记中。
eval(document.querySelector('div').innerText);
var sel = document.querySelector('select'),
html = '';
for (var item in convertedArray) {
html += '<option value="' + item + '">' + convertedArray[item] + '</option>';
}
sel.innerHTML = html;
<div>
var convertedArray = {
'VAS_1000' : 'AMC',
'VAS_1001' : 'EW',
'VAS_1002' : 'EW',
}
</div>
<select>
</select>
你能分享你所到目前为止已经试过? – Rajesh