ItSolutionStuff.com

AngularJS Remove Duplicates Object from Array Example

By Hardik Savani • October 20, 2023
Javascript Angular

Today, i will give you example of how to remove duplicate array or object from array using js forEach. we can simply filter remove duplicates from array in angular js. you use this code for removing same entries array in json object in angular 2, angular 4, angular 6, angular 7.

in this example, we will create simple angular js json array with some duplicate array. then we will create javascript function that will remove duplicate array from our json object array. so just use bellow example and do it.

Example:

<!doctype html>

<html>

<head>

<title>Angularjs remove duplicates object from objects array - ItSolutionStuff.com</title>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

</head>

<body ng-app='myapp'>

<h1>Angularjs remove duplicates object from objects array - ItSolutionStuff.com</h1>

<div ng-controller="myCtrl">

</div>

</body>

<script type="text/javascript">

var app = angular.module('myapp', []);

app.controller('myCtrl', function ($scope, $location) {

var myArray = [

{ "id" : "1", "firstName" : "Hardik", "lastName" : "Savani" },

{ "id" : "2", "firstName" : "Vimal", "lastName" : "Kashiyani" },

{ "id" : "3", "firstName" : "Harshad", "lastName" : "Pathak" },

{ "id" : "1", "firstName" : "Hardik", "lastName" : "Savani" },

{ "id" : "5", "firstName" : "Harsukh", "lastName" : "Makawana" }

];

console.log(removeDumplicateValue(myArray));

});

function removeDumplicateValue(myArray){

var newArray = [];

angular.forEach(myArray, function(value, key) {

var exists = false;

angular.forEach(newArray, function(val2, key) {

if(angular.equals(value.id, val2.id)){ exists = true };

});

if(exists == false && value.id != "") { newArray.push(value); }

});

return newArray;

}

</script>

</html>

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

AngularJS Check Checkbox is Checked or Not Example

Read Now →

PHP AngularJS Add Remove Input Fields Dynamically Example

Read Now →

How to Copy to Clipboard without Flash in AngularJS?

Read Now →

AngularJS Scroll to a Specific Element using Anchorscroll

Read Now →

AngularJS Convert Comma Separated String to Array Example

Read Now →

AngularJS Nl2br Filter for Textarea Content Example

Read Now →

AngularJS Display Default Image If Original Does Not Exists Example

Read Now →

AngularJS Update Bind Ng-model Input Value from JQuery Example

Read Now →

How to Call AngularJS Controller Function in JQuery?

Read Now →

How to Hide Div After Some Time in AngularJS?

Read Now →

How to Call Function on Page Load in AngularJS?

Read Now →

AngularJS - How to Limit String Length using Filter?

Read Now →

AngularJS - How to Capitalize the First Letter of Word Example

Read Now →

How to Remove HTML Tags from String in AngularJS?

Read Now →