# Remove duplicates from an array of objects

**URL:** https://discourse.nodered.org/t/remove-duplicates-from-an-array-of-objects/28013
**Category:** General
**Created:** [8 June 2020 21:07 UTC](https://discourse.nodered.org/t/remove-duplicates-from-an-array-of-objects/28013 "2020-06-08T21:07:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [8 June 2020 21:07 UTC](https://discourse.nodered.org/t/remove-duplicates-from-an-array-of-objects/28013/1 "2020-06-08T21:07:26Z")

</div>

> [@Dashboard Charts - 7 day rolling average](https://discourse.nodered.org/t/dashboard-charts-7-day-rolling-average/27145/11):
>
> looking at your original data - why do you have duplicate x values ?

Talk about a self fulfilling prophecy!!  
I've noticed that duplicate entries have started to appear in my data feed, which are creating errors in a 7 day rolling average calculation.

So I'm getting now something like;

```auto
[{
	"x": 1591315200000,
	"y": 8
}, {
	"x": 1591315200000,
	"y": 8
}, {
	"x": 1591315200000,
	"y": 8
}, {
	"x": 1591228800000,
	"y": 30
}]

```

So 3 of the 4 entries are identical, and I need just a unique entry for each timestamp, like;

```auto
[{
	"x": 1591315200000,
	"y": 8
}, {
	"x": 1591228800000,
	"y": 30
}]

```

I've googled, and found several solutions, but not really found one that works.  
Is there an easy way to filter out the duplicates?

---

<div class="post-metadata">

### Author: ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)
#### Post date: [9 June 2020 04:17 UTC](https://discourse.nodered.org/t/remove-duplicates-from-an-array-of-objects/28013/2 "2020-06-09T04:17:30Z")

</div>

I would take the lazy approach and use a function node.

```auto
var prev = null;

msg.payload = msg.payload.filter((item) => {
    var keep = (item.x !== prev);
    prev = item.x;
    return keep;
});

return msg;

```

This assumes you only want to dedupe on 'x', and they are all in sequence. You would have to modify it a bit if that's not the case.

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [9 June 2020 08:37 UTC](https://discourse.nodered.org/t/remove-duplicates-from-an-array-of-objects/28013/3 "2020-06-09T08:37:52Z")

</div>

Perfect! thanks Michael.

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [23 June 2020 08:37 UTC](https://discourse.nodered.org/t/remove-duplicates-from-an-array-of-objects/28013/4 "2020-06-23T08:37:55Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
