Here are some arrays of objects I am working with:
var arr = [
{name: 'john', last: 'smith'},
{name: 'jane', last: 'doe'},
{name: 'johnny', last: 'appleseed'},
{name: 'johnson', last: 'smith'},
{name: 'jane', last: 'smith'}
]
My goal is to eliminate duplicates based on the key name
. The expected output should be:
var arr = [
{name: 'john', last: 'smith'},
{name: 'jane', last: 'doe'},
{name: 'johnny', last: 'appleseed'},
{name: 'johnson', last: 'smith'}
]
I attempted to achieve this using the following method:
function _unique(arr) {
let uniqueArr = [];
for (var i = 0; i < arr.length; i++) {
for(var j=i+1; j<arr.length; j++) {
if(arr[i].name.indexOf(arr[j].name) == -1) {
uniqueArr.push(arr[j])
}
}
}
return uniqueArr;
}
console.log(_unique(arr))
Unfortunately, the program did not produce the desired result.
Any help or guidance would be greatly appreciated!