Reputation: 176
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 ?
Upvotes: 0
Views: 4408
Reputation: 22524
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; }
Upvotes: 3