0
的鍵值對,我有以下對象:
var business_challenges =[{title: 'Business Challenges'}, [{digital_trans: 'Digital Transformation'}, {agile_mobile:'Agile & Mobile Working always_on'}, {always_on:'Always on Infrastructure'}, {connect_protect: 'Connect & Protect'}, {cost_cutting:'Cost Cutting/Maximise Investment'}, {improving_cust: 'Improving Customer Engagement'} ]];
var business_divisions = [{title: 'Business Divisions'},[{SMB:'SMB'}, {DCS:'DCS'}, {DPS:'DPS'}]];
var filters = $.merge(business_divisions, business_challenges);
我通過對象試圖循環拿到鑰匙:值對,但我掙扎。 Key值是數字而不是關聯數組鍵,值是一個對象。我試圖嵌套另一美元,但這是行不通的。
任何人都可以協助嗎?我是否需要更改過濾器對象的放置方式?
var filter_html = '<ul>';
//var filtersJSON = $.parseJSON(filters);
$.each(filters, function(i, data) {
var filter_title = data.title; //THIS WORKS
filter_html = filter_html+filter_title;
$.each(data, function(key, val) {
filter_html = filter_html+'<li><input type="checkbox" value="'+ key +'">'+ val.key +'</li>'; //THIS DOESNT WORK
});
});
filter_html = filter_html+ '</ul>';
$('#filterControls').html(filter_html);
該數據結構是很奇怪的。你控制它的來源? – charlietfl
是的,如果需要,我可以改變它。這是我第一次嘗試在Javascript中放置一個關聯數組/對象。我更習慣於PHP,所以我有點困惑! – LeeTee
我認爲你應該使用'$ .extend()'i.p.o. $ .merge()。 – Franco