先上代码,formatter希望返回组装后的数据var olda = [ { flight: 'hu7305', from: 'lhw', to: 'ybp' }, { flight: 'hu7306',//new中没有,state=del from: 'lhw', to: 'ybp' },];var newa = [ { flight: 'hu7305',//old中有,忽略 from: 'lhw', to: 'ybp' },{ flight: 'hu7777',//old中没有,state=add from: 'www', to: 'qqq' }];function formatter(a, b){ ... return ([//希望返回如下的数据 { flight: 'hu7306',//del from: 'lhw', to: 'ybp', state: 'del' },{ flight: 'hu7777',//add from: 'www', to: 'qqq', state: 'add' } ])}formatter(olda, newa);规则:单个对象中的flight、from、to三者的值任一不同则视为不同的数据,需要返回;在olda中有而newa中没有时state为del,在olda没有而newa中有则state为add。
1 回答
慕田峪7331174
TA贡献1828条经验 获得超13个赞
function formatter (a, b) {
const m = new Map(a.map(x => [x.flight, x]))
b.forEach(x => m.set(x.flight, m.has(x.flight) ? null : Object.assign({state: 'add'}, x)))
return Array.from(m.values())
.filter(x => x)
.map(x => x.state ? x : Object.assign({state: 'del'}, x))
}
添加回答
举报
0/150
提交
取消