const people = [
{firstName: 'Eileen', lastName: 'Graham'},
{firstName: 'Peter', lastName: 'Graham'},
{firstName: 'Jared', lastName: 'Miller'},
{firstName: 'Robert', lastName: 'Williams'},
{firstName: 'Robert', lastName: 'Jolley'},
{firstName: 'Robert', lastName: 'Graham'},
{firstName: 'David', lastName: 'Jennings'},
];
[ 'Eileen', 'Peter', 'Jared', 'Robert', 'David' ]
const people = [
{firstName: 'Eileen', lastName: 'Graham'},
{firstName: 'Peter', lastName: 'Graham'},
{firstName: 'Jared', lastName: 'Miller'},
{firstName: 'Robert', lastName: 'Williams'},
{firstName: 'Robert', lastName: 'Jolley'},
{firstName: 'Robert', lastName: 'Graham'},
{firstName: 'David', lastName: 'Jennings'},
];
let getFirstName = people.map( el => el.firstName);
let getUniqueNames = (arr) => {
let result = [];
for (let item of arr) {
if (!result.includes(item)) {
result.push(item);
}
}
return result;
}
console.log(getUniqueNames(getFirstName));
//[ 'Eileen', 'Peter', 'Jared', 'Robert', 'David' ]
const people = [
{firstName: 'Eileen', lastName: 'Graham'},
{firstName: 'Peter', lastName: 'Graham'},
{firstName: 'Jared', lastName: 'Miller'},
{firstName: 'Robert', lastName: 'Williams'},
{firstName: 'Robert', lastName: 'Jolley'},
{firstName: 'Robert', lastName: 'Graham'},
{firstName: 'David', lastName: 'Jennings'},
];
const getUniqueByKey = (array, key) => {
const result = []
array.forEach((v) => {
const keyValue = v[key]
const repeatsCount = array.filter((_) => _[key] === keyValue).length
if (repeatsCount === 1) {
result.push(v)
}
})
return result
}
getUniqueByKey(people, 'firstName')
[
{ firstName: 'Eileen', lastName: 'Graham' },
{ firstName: 'Peter', lastName: 'Graham' },
{ firstName: 'Jared', lastName: 'Miller' },
{ firstName: 'David', lastName: 'Jennings' }
]