我有这样的json结构:
{
"maxPageIndex":1,
"totalItems":1,
"currentPageIndex":1,
"currentPage":[
{"id":1,"name":"Hummer","compareItems":
[{"companyId":1,"companyName":"Makoto","compareItemId":10,"compareItemName":"MyHummer"},
{"companyId":3,"companyName":"Boshst","compareItemId":11,"compareItemName":"Hummer tools"}]
},
{"id":2,"name":"Refrigerator","compareItems":
[{"companyId":1,"companyName":"LeGe","compareItemId":12,"compareItemName":"iRefrigerator"},
{"companyId":2,"companyName":"Condi","compareItemId":13,"compareItemName":"Refrigerator P48"},
{"companyId":3,"companyName":"Boshst","compareItemId":14,"compareItemName":"Refrigerator N1"},
{"companyId":4,"companyName":"Atlanta","compareItemId":15,"compareItemName":"_Refrigerator"},
{"companyId":5,"companyName":"ChinWeui","compareItemId":16,"compareItemName":"Refrigerator super"}]
},
{"id":3,"name":"eBook","compareItems":
[{"companyId":2,"companyName":"Condi","compareItemId":17,"compareItemName":"iBook"}]
}
]}
我应该在表中显示相同结构的主要问题......
我的意思是currentPage中的每个项目都在表compareItems中显示。但!有不一样的长度,我的桌子就像楼梯,我需要把它做成相同的大小,我的意思是,例如
最大长度在Refrigerator
对象(5)中,而悍马和电子书的compareItems也应该是5长度(只是具有公司数据的emty对象,通过Id进行排序)。
结果我需要这样的表格:
{
"maxPageIndex":1,
"totalItems":1,
"currentPageIndex":1,
"currentPage":[
{"id":1,"name":"Hummer","compareItems":
[{"companyId":1,"companyName":"LeGe","compareItemId":10,"compareItemName":"MyHummer"},
{"companyId":2,"companyName":"Condi"},
{"companyId":3,"companyName":"Boshst","compareItemId":11,"compareItemName":"Hummer tools"},
{"companyId":4,"companyName":"Atlanta"},
{"companyId":5,"companyName":"ChinWeui"}]
},
{"id":2,"name":"Refrigerator","compareItems":
[{"companyId":1,"companyName":"LeGe","compareItemId":12,"compareItemName":"iRefrigerator"},
{"companyId":2,"companyName":"Condi","compareItemId":13,"compareItemName":"Refrigerator P48"},
{"companyId":3,"companyName":"Boshst","compareItemId":14,"compareItemName":"Refrigerator N1"},
{"companyId":4,"companyName":"Atlanta","compareItemId":15,"compareItemName":"_Refrigerator"},
{"companyId":5,"companyName":"ChinWeui","compareItemId":16,"compareItemName":"Refrigerator super"}]
},
{"id":3,"name":"eBook","compareItems":
[{"companyId":1,"companyName":"LeGe"},
{"companyId":2,"companyName":"Condi","compareItemId":17,"compareItemName":"iBook"},
{"companyId":3,"companyName":"Boshst","compareItemId":11,"compareItemName":"Hummer tools"},
{"companyId":4,"companyName":"Atlanta"},
{"companyId":5,"companyName":"ChinWeui"}]
}
]}
有点清楚吗?)
主要目标:我是js的初学者,我写了这样的功能(我正在使用lodash lib):
ctrl.companys = [];
ctrl.products = /*data from question*/
_.each(ctrl.products.currentPage, function (rootEl) {
_.each(rootEl.compareItems, function (compareItemsElement) {
if (!_.some(ctrl.companys, {Id: compareItemsElement.companyId}) && !_.isUndefined(compareItemsElement.companyId)){
ctrl.companys.push({Id: compareItemsElement.companyId, Name: compareItemsElement.companyName});
}
});
});
ctrl.companys = _.sortBy(ctrl.companys, 'Id');
_.each(ctrl.products.currentPage, function (rootEl, rootElIndex) {
_.each(rootEl.compareItems, function (compareItemsElement, compareItemsElementIndex) {
_.each(ctrl.companys, function (company, companyIndex) {
if (!_.some(rootEl.compareItems, {companyId: company.Id})){
ctrl.products.currentPage[rootElIndex].compareItems.push({companyId: company.Id, compareItemName: '-'});
ctrl.products.currentPage[rootElIndex].compareItems = _.sortBy(ctrl.products.currentPage[rootElIndex].compareItems, 'companyId');
}
});
});
});
它有效...... 但! 我认为这段代码很丑陋......也许还有更好的方法吗?