Sometimes in your every day of programming you get an array of objects which are not unique 😰.
You have an array of objects where some of them are duplicates but with different object properties, and you want to get only one object with all of those properties.
In JavaScript
:
Let’s see the actual code problem:
[
{
label: "Book1",
data: "US edition"
},
{
label: "Book1",
title: "UK edition"
},
{
label: "Book1",
date: "12.2.2020"
},
{
label: "Book2",
data: "CAN edition"
},
{
label: "Book2",
title: "test edition"
}
];
You want to see this kind of data:
[
{
label: "Book1",
data: "US edition",
title: "UK edition",
date: "12.2.2020",
etc: "etc..."
},
{
label: "Book2",
data: "CAN edition",
title: "test edition"
}
];
Firstly let’s see which data structure we can use to help us achieve this approach, e.g. we can use Map 🤩.
Map is a collection of keyed data items, just like an Object
. But the main difference is that Map
allows keys of any type.
If your object property is of any kind it will work with Map :D.
First, consider making an interface for your object:
class: IBook {
label: string;
data: string;
title: string;
date: Date;
etc: Etc;
}
#software #coding #typescript #programming #javascript