0

This question is asked a lot, but any solution that i've tried didn't work for me. Maybe used it wrong.

I want to filter duplicated object inside an array i've tried it with forEach(), find(), filter(), and checking it based on indexOf() but I coulndt make it work. So I hope someone can help me here with a good explanation how it must be done. I have couple more of the functionality

MOST RECENT CODE

var list = [{
        name: "Lisa",
        profile: "admin"
    }, {
        name: "Eliza",
        profile: "admin"
    }, {
        name: "Lisa",
        profile: "admin"
    }];

function log(a) {
    console.log(a)
}
var arr = list.filter(function(item) {
    return item !== list
})

log(arr)

So this must return

[{ name: "Lisa", profile: "admin" }, { name: "Eliza", profile: "admin" }];

Because Lisa is a duplicate object.

J. Doe
  • 11
  • 3
  • 1
    How do you define "duplicate"? Having all the same properties with the same values, or just having the same `name`, or...? What is the desired output, an array with no duplicates, or a list of which items *are* duplicates? – nnnnnn Aug 02 '16 at 02:16
  • @nnnnnn, the one with the same object, name && profile – J. Doe Aug 02 '16 at 02:18
  • 2
    In your example they're not the *same* object, they're two separate objects with the same properties as each other. – nnnnnn Aug 02 '16 at 02:19
  • @nnnnnn, ah i see object cant be checked by eqaul to another object. How can I filter it ? – J. Doe Aug 02 '16 at 02:29
  • list.reduce((carry, item, ind) => { if (ind == 0 || carry.map(JSON.stringify).indexOf(JSON.stringify(item)) == -1) { carry.push(item) } return carry }, []) – wonyeouuu Aug 02 '16 at 02:45
  • @wonyeouuu,this gives me error on node.js – J. Doe Aug 02 '16 at 03:51
  • Maybe it's caused by the arrow function, check this [modified one](https://codepen.io/wonyeouuu/pen/bZxZOW) – wonyeouuu Aug 02 '16 at 04:12

0 Answers0