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 ?

2
  • 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 to learn how to write effective questions. Commented Nov 7, 2017 at 4: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 ... Commented Nov 7, 2017 at 4:45

1 Answer 1

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; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.