const arr = [
{ id: 1, name: 'Alexey', country: 'Moscow' },
{ id: 2, name: 'Mikhail', country: 'Tula' },
{ id: 3, name: 'Vladimir', country: 'Moscow' },
{ id: 4, name: 'Andrey', country: 'Vladivostok' },
{ id: 5, name: 'Andrey', country: 'Moscow' },
{ id: 6, name: 'Peter', country: 'Vladivostok' },
{ id: 7, name: 'Sergey', country: 'Tula' }
]
const arr = [
{ id: 1, country: 'Moscow' },
{ id: 2, country: 'Tula' },
{ id: 3, country: 'Vladivostok' }
]
const newArray = arr.reduce((result, elem) => {
const alreadyExist = Boolean(result.find(qwerty => qwerty.country === elem.country));
if(!alreadyExist) {
result.push({
id: result.length + 1,
country: elem.country
});
}
return result;
}, []);
const newArr = arr
.filter(function(n) {
return !(this[n.country] = this.hasOwnProperty(n.country));
}, {})
.map((n, i) => ({ id: i + 1, country: n.country }));
const newArr = Object.values(arr.reduce((acc, { country }) => {
acc[0][country] = acc[0][country] || { id: ++acc[1], country };
return acc;
}, [ {}, 0 ])[0]);
const newArr = Array.from(arr.reduce((acc, { country: n }) => (
acc.set(n, acc.get(n) || { id: -~acc.size, country: n })
), new Map).values());