This is the array of javascript objects. I want these javascript objects will merge into a single javascript object according to their same property value and remaining values come into another array of named info.
This is the original array:
const array = [
{
price: 27,
colors: "BLACK",
size: "S"
},
{
price: 23,
colors: "GREEN",
size: "S"
},
{
price: 0,
colors: "GREEN",
size: "M"
},
{
price: 0,
colors: "OLIVE",
size: "S"
},
{
price: 65,
colors: "RED",
size: "S"
},
{
price: 12,
colors: "RED",
size: "M"
},
{
price: 12,
colors: "BLACK",
size: "L"
},
{
price: 34,
colors: "RED",
size: "L"
},
{
price: 43,
colors: "OLIVE",
size: "M"
},
{
price: 23,
colors: "OLIVE",
size: "L"
},
{
price: 34,
colors: "GREEN",
size: "L"
},
]
This is the required array:
[
{
colors: "BLACK",
info: [{size: "S", price: 27}, {size: "L", price: 12}]
},
{
colors: "GREEN",
info: [{size: "S", price: 23}, {size: "M", price: 0}, {size: "L", price: 23}]
},
{
colors: "RED",
info: [{size: "S", price: 65}, {size: "M", price: 12}, {size: "L", price: 34}]
},
{
colors: "OLIVE",
info: [{size: "S", price: 0}, {size: "M", price: 43}, {size: "L", price: 23}]
},
]
Kindly provide me with some solutions. Thanks