1 回答
TA贡献1906条经验 获得超10个赞
拥有 , SearchResultOne,SearchResultTwo等是可能的,但比它的价值更难SearchResultThree,因此将它放入数组中更有意义:
const inp = [ { email: 'one@xyz.de',
SearchQuery: 'Abts, Tomma',
SearchResult: 1 },
{ email: 'one@xyz.de',
SearchQuery: 'Ernst, Max',
SearchResult: 3},
{ email: 'one@xyz.de',
SearchQuery: 'Sigmund Abeles ',
SearchResult: 1 },
{ email: 'two@xyz.de',
SearchQuery: 'Barlach',
SearchResult: 4 } ];
const oup = inp.reduce((acc, o) =>
{
const queryResult = acc.find(qr => qr.email == o.email);
if(queryResult)
{
queryResult.results.push({SearchResult:o.SearchResult, SearchQuery: o.SearchQuery})
}
else
{
let newQR = {email: o.email, results: [{SearchResult:o.SearchResult, SearchQuery: o.SearchQuery}]};
acc.push(newQR);
}
return acc;
}, []);
console.log(JSON.stringify(oup));
添加回答
举报