my array filter does not work and I not sure which part was wrong. My sample data:
var arr = [
{accountID: '-KqIR-HT7orcPpe-lZa8', age: 31, gender: 'female'},
{accountID: '-KqIR-GvEpHFiPFZRxbG', age: 59, gender: 'female'},
{accountID: '-KqIR-GvEpHFiPFZRxbG', age: 59, gender: 'female'},
{accountID: '-KqIR-GvEpHFiPFZRxbG', age: 59, gender: 'female'},
{accountID: '-KqIR-GvEpHFiPFZRxbG', age: 59, gender: 'female'},
{accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'},
{accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'},
{accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'},
{accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'},
{accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female'},
{accountID: '-KqIR-EKbN02zAfCRyoe', age: 24, gender: 'female}
];
I wanted to filter out the same accountID. I found this solution online:
arr = arr.filter( function( item, index, inputArray ) {
return inputArray.indexOf(item) == index;
});
When I tried to print out the filtered array using this:
for(var i = 0; i < arr.length; i++){
console.log(arr[i][0].accountID + ' ' + arr[i][0].age + ' ' + arr[i][0].gender);
}
I get back the exact same array with duplicate results. Which part was wrong? Thanks!
arrthe same objects (literally the same, not structurally the same) asinputArray?accountIDis the same or that all three fields are the same. You cannot find duplicates usinginputArray.indexOf(item) == indexbecause each of the objects you have in the array is an unique object, even if they have the same values.