The Easy Way to Create A Unique Array Of JSON Objects in JavaScript

The Easy Way to Create A Unique Array Of JSON Objects in JavaScript

You don’t need to use jQuery, lodash, map, filter or the dreaded ‘for’ loop.

Google JS array of unique JSON Objects and you’ll be plunged into a world of panicked StackOverflow answers that range from superfluous to ridiculous — third-party libraries, custom iterator functions, unportable code that’s difficult to read…

You don’t need any of it!

EDIT: Yes, I fully appreciate the irony of this article now appearing on page one of that Google search. I should’ve seen that one coming 🤦🏻‍♂️

Before I continue, let me be clear about the exact problem we’re trying to solve. Consider the following JSON array:

const people = [
  {
    "name": "John", "age": 25,
  }, {
    "name": "Jane", "age": 30,
  }, {
    "name": "John", "age": 25,
  }, {
    "name": "Jim", "age": 35,
  }
]

As you can see, we’ve got a duplicate Object for the person named John. If your application requires an array of unique Objects, you’ll need to find a way to filter out the duplicate entries.

javascript javascript-tips coding

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

7 Tips to Write Clean JavaScript Code

Who Else Wants to Write Clean JavaScript Code? In this Javascript tutorial, you'll see 7 Tips to Make Your Coworkers Fall in Love With Your Code.

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

5 Useful JavaScript Tips to Speed Up Your Coding

5 ways and tips to code faster in JavaScript.

7 Simple JavaScript Tips for Optimizing Your Code

You may not know about these tips 7 Simple JavaScript Tips for Optimizing Your Code

Who Else Wants to Write Clean JavaScript Code?

Who Else Wants to Write Clean JavaScript Code? 7 Tips to Make Your Coworkers Fall in Love With Your Code.