// 字段描述var field = [{ field: '服务器', display: 'host', visible: true, type: 'checkbox'}, { field: '部门', display: 'department', visible: true, type: 'input'}]// 原始list数据var list = [{ id: 1, host: '1.1 .1 .1', department: '技术'}]将两个数组合并,并返回类似于以下形式的数组[{ id: 1, host: { field: '服务器', display: 'host', value: '1.1 .1 .1', visible: true, type: 'checkbox' }, department: { field: '部门', display: 'department', value: '技术', visible: true, type: 'input' } }]
4 回答
饮歌长啸
TA贡献1951条经验 获得超3个赞
const result = list.map((item) => {
Object.keys(item).forEach((key) => {
const filterArr = field.filter((fieldItem) => fieldItem.display === key)
if (filterArr.length > 0) {
filterArr[0].value = item[key];
item[key] = filterArr[0];
}
});
return item;
});
console.log(result);
慕桂英546537
TA贡献1848条经验 获得超10个赞
aluckdog
TA贡献1847条经验 获得超7个赞
嗨,思路供参考
const fieldMap = new Map(field.map(d => [d.display, d]))
const result = list.map(x =>
Object.keys(x).reduce((o, k) => {
o[k] = fieldMap.has(k) ? { ...fieldMap.get(k), value: x[k] } : x[k]
return o
}, {})
)
添加回答
举报
0/150
提交
取消