0

I have my json Array input like this :-

var temp =  [{first: 569, second: "789", third: "458"},  {first: value1, 476: "147", third: "369"},  {first: 100, second: "200", third: "300"},  {first: 100, second: "200", third: "300"},   {first: 100, second: "200", third: "300"}];

I want to remove duplicates from this and need an output like :

var temp =  [{first: 569, second: "789", third: "458"},  {first: value1, 476: "147", third: "369"},  {first: 100, second: "200", third: "300"}];

How can i achieve this ?

User09111993
  • 176
  • 2
  • 14
  • 3
    You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – Sudheesh Singanamalla Nov 07 '17 at 04:06
  • No one will post a question here before researching. I tried lots of things but didn't get what i want thats why i posted here for help ... – User09111993 Nov 07 '17 at 04:45

1 Answers1

3

You can use array#map and JSON.stringify to create string from object and then use Set to get all unique string and then convert back to object using array#map and JSON.parse().

Following proposal won't work if the data type isn't JSON.

const temp =  [{first: 569, second: "789", third: "458"},  {first: 476, second : "147", third: "369"},  {first: 100, second: "200", third: "300"},  {first: 100, second: "200", third: "300"},   {first: 100, second: "200", third: "300"}];

const result = [...new Set(temp.map(obj => JSON.stringify(obj)))]
                 .map(str => JSON.parse(str));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51