вот оно с Array Reduce
const products=
{ 100: [ 'abc', 'xyz', 'mno', 'pqr']
, 200: [ 'mno', 'pqr']
, 300: [ 'abc', 'xyz']
, 400: [ 'abc', 'pqr']
}
var result= Object.keys(products).reduce((acc, elm)=>
{
for (let ref of products[elm])
{
if (!acc[ref]) acc[ref]= [Number(elm)]
else acc[ref].push(Number(elm))
}
return acc
}
,{})
for (trace in result) console.log(trace, ':', JSON.stringify(result[trace]))
то же самое с двойным уменьшением, как @ Bergi спросил в своем комментарии:
const products=
{ 100: [ 'abc', 'xyz', 'mno', 'pqr']
, 200: [ 'mno', 'pqr']
, 300: [ 'abc', 'xyz']
, 400: [ 'abc', 'pqr']
}
var result = Object.keys(products).reduce((acc, elm) =>
products[elm].reduce((acc, ref) =>
{
if (!acc[ref]) acc[ref]= [Number(elm)]
else acc[ref].push(Number(elm))
return acc
}
, acc)
, {})
for (trace in result) console.log(trace, ':', JSON.stringify(result[trace]))