Recently, I received an array from an API that has the following structure:
results = [
{name: 'Ana', country: 'US', language: 'EN'},
{name: 'Paul', country: 'UK', language: 'EN'},
{name: 'Luis', country: 'PH', language: 'SP'},
{name: 'Tom', country: 'US', language: 'EN'}
];
I want to transform this array into a new one that looks like this:
countries = [
{filter: 'country', value: 'PH'},
{filter: 'country', value: 'UK'},
{filter: 'country', value: 'US'},
];
To achieve this, I attempted the following code snippet:
countries = Array.from([...new Set(this.results.map(item => ({categoryOfFilter: 'country', value: item.country})))]);
However, despite using the set, the resulting array contains duplicates. It ends up looking like this:
countries = [
{filter: 'country', value: 'US'},
{filter: 'country', value: 'UK'},
{filter: 'country', value: 'PH'},
{filter: 'country', value: 'US'},
];
Do you have any insights or suggestions for solving this issue? Javascript has never been my strong suit so any help is greatly appreciated.