Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery$.each()-努力获取对象的键值对_Jquery_Loops_Object_Each_Associative Array - Fatal编程技术网

JQuery$.each()-努力获取对象的键值对

JQuery$.each()-努力获取对象的键值对,jquery,loops,object,each,associative-array,Jquery,Loops,Object,Each,Associative Array,我的目标如下: 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'},

我的目标如下:

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); 
我试图在对象中循环以获得键:值对,但我很挣扎。键值是数字而不是关联数组键值,并且该值是对象。我试着每只嵌套另一美元,但这不起作用

有人能帮忙吗?是否需要更改过滤器对象的组合方式

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);
var filter_html='
    '; //var filtersJSON=$.parseJSON(过滤器); $。每个(过滤器、函数(i、数据){ var filter_title=data.title;//这很有效 filter\u html=filter\u html+filter\u title; $。每个(数据、函数(键、值){ filter_html=filter_html+'
  • '+val.key+'
  • ';//这不起作用 }); }); filter_html=filter_html+'
'; $('filterControls').html(filter#uhtml);
以便

获取键:值对 您可以测试每个循环中的每个元素

实际上,对象过滤器包含对象和对象数组

对于数组元素,可以使用以下方法获取当前对象值:

var key = Object.keys(val)[0];  // get the key name
var value = val[key];   // from the key name you can get the value
这是因为数组中的每个对象都有不同的属性名

片段:

var business_challenges=[{title:'business challenges'},[{digital_trans:'digital Transformation'},{agile_mobile:'agile&mobile Working everys'},{everys on:'everys on Infrastructure'},{connect protect:'connect and protect'},{cost_cutting:'cost cutting/maximitize Investment'},{改善客户参与度'}];
var business_divisions=[{title:'business divisions'},[{SMB:'SMB'},{DCS:'DCS'},{DPS:'DPS'}];
var filters=$.merge(业务部门、业务挑战);
var filter_html=“
    ”; $。每个(过滤器、函数(i、数据){ if(Object.prototype.toString.call(data)='[Object Array]'){ $。每个(数据、函数(键、值){ var key=Object.keys(val)[0]; var值=val[键]; filter_html=filter_html+'
  • '+value+'
  • '; log('objectn.+key+':'+JSON.stringify(val)); }); }否则{ filter\u html=filter\u html+data.title; } }); filter_html=filter_html+'
'; $('filterControls').html(filter#uhtml)


那个数据结构很奇怪。你控制它的来源吗?是的,如果需要我可以改变它。这是我第一次尝试在Javascript中组合一个关联数组/对象。我更习惯PHP,所以我有点困惑!我认为您应该使用
$.extend()
I.p.o.$.merge()。您当前的问题是,您需要另一个嵌套循环来迭代每个子数组中的子数组,而合并或扩展将使事情变得更加复杂。建议您返回源代码,并以更一致的方式构造它。