注意:一般不会这样从后端拿数据,其本意就是整合数据。
/**
请你写一段代码
将原始数据「filterList」与「selectedOptions」结合
根据selectedOptions为true的项,产生与期望结果相符的结果
*/
//原始数据
const filterList = [
{
Code: "1",
Id: "1",
List: [
{Code: '00', Name: '通用'},{Code: '21', Name: '15秋'},{Code: '22', Name: '15冬'},
{Code: '23', Name: '16春'},{Code: '24', Name: '16夏'},{Code: '25', Name: '16秋'},
{Code: '26', Name: '16冬'},{Code: '27', Name: '17春'},{Code: '28', Name: '17夏'},
{Code: '29', Name: '17秋'},{Code: '30', Name: '17冬'},{Code: '31', Name: '18春'},
{Code: '32', Name: '18夏'},{Code: '33', Name: '18秋'},{Code: '34', Name: '18冬'},
{Code: '35', Name: '19春'},{Code: '36', Name: '19夏'},{Code: '37', Name: '19秋'},
{Code: '38', Name: '19冬'},{Code: '40', Name: '20夏'},{Code: '41', Name: '20秋'},
{Code: '45', Name: '20冬'},{Code: '46', Name: '21秋'},{Code: '47', Name: '21冬'},
{Code: '48', Name: '21春'},{Code: '49', Name: '21夏'},{Code: '50', Name: '22春'},
{Code: '51', Name: '22夏'},{Code: '52', Name: '22冬'},{Code: '53', Name: '22秋'},
{Code: '54', Name: '2022春'},{Code: '55', Name: '22秋冬'}
],
ListType: "Base",
Mode: "alone",
Name: "销售季节",
Type: "",
inputType: "text",
},
{
Code: "2",
Id: "2",
List: [
{Code: 'DA', Name: '订货会订单'},{Code: 'DH', Name: '期货订单'},{Code: 'XS', Name: '档口订单'}
],
ListType: "Base",
Mode: "alone",
Name: "单据类型",
Type: "",
inputType: "text",
},
{
Code: "3",
Id: "3",
List: [
{Code: '0', Name: '未审核'},{Code: '1', Name: '已审核'},{Code: '2', Name: '部分发'},
{Code: '3', Name: '已发完'},{Code: '4', Name: '已完成'},{Code: '5', Name: '已作废'}
],
ListType: "Base",
Mode: "alone",
Name: "单据状态",
Type: "",
inputType: "text",
},
{
Code: "4",
Id: "4",
List: [
{Code: '0', Name: 'OEM'},{Code: '1', Name: 'TOB'},{Code: '2', Name: '档口'},
{Code: '3', Name: '订货会'}
],
ListType: "Base",
Mode: "alone",
Name: "单据来源",
Type: "",
inputType: "text",
},
{
Code: "5",
Id: "5",
List: [
{Code: '01', Name: '订货'},{Code: '02', Name: '订货买断'},{Code: '03', Name: '订货追单'}
],
ListType: "Base",
Mode: "alone",
Name: "订货类型",
Type: "",
inputType: "text",
},
];
const selectedOptions = {
"1_00": true,
"1_49": false,
"2_DH": true,
"2_XS": true,
"3_1": true,
"4_1": true,
};
//期望结果
const expectedResult = [
{
Code: "1",
ListType: "Base",
Name: "销售季节",
Type: "",
Val: [
{code: '00', val: '通用'}
],
inputType: "text"
},
{
Code: "2",
ListType: "Base",
Name: "单据类型",
Type: "",
Val: [
{code: 'DH', val: '期货订单'},
{code: 'XS', val: '档口订单'}
],
inputType: "text"
},
{
Code: "3",
ListType: "Base",
Name: "单据状态",
Type: "",
Val: [
{code: '1', val: '已审核'}
],
inputType: "text"
},
{
Code: "4",
ListType: "Base",
Name: "单据来源",
Type: "",
Val: [
{code: 'TOB', val: 'TOB'}
],
inputType: "text"
}
];
//请在此处写下你的代码,并提交保存后的链接/截图你的代码
let selectKeys = {}
Object.keys(selectedOptions).forEach(item=>{
if(selectedOptions[item]){
let key = item.split("_")
if(selectKeys[key[0]]){
selectKeys[key[0]].push(key[1])
}else{
selectKeys[key[0]] = [key[1]]
}
}
})
let mergeArry = []
for( key in selectKeys){
let filterArry = filterList.map(item=>{
if(item.Code == key){
let obj = {
Code: item.Code,
ListType: item.ListType,
Name: item.Name,
Type: item.Type,
Val: [],
inputType:item.inputType
}
selectKeys[key].forEach(itemKey=>{
item.List.forEach(lsitem=>{
if(lsitem.Code == itemKey){
obj.Val.push({
code:itemKey,
val:lsitem.Name
})
}
})
})
return obj
}
}).filter(items=>{
return items
})
mergeArry = mergeArry.concat(filterArry)
}
console.log(mergeArry)