how to remove duplicate array object in javascript
JavaScript
let person = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"}];
function removeDuplicates(data, key) {
return [
...new Map(data.map(item => [key(item), item])).values()
]
};
console.log(removeDuplicates(person, item => item.name));let days = ["senin","senin","selasa","selasa","rabu","kamis", "rabu"];
let fullname = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"},{name: "jane"}];
// REMOVE DUPLICATE FOR ARRAY LITERAL
const arrOne = new Set(days);
console.log(arrOne);
const arrTwo = days.filter((item, index) => days.indexOf(item) == index);
console.log(arrTwo);
// REMOVE DUPLICATE FOR ARRAY OBJECT
const arrObjOne = [...new Map(person.map(item => [JSON.stringify(item), item])).values()];
console.log(arrObjOne);
const arrObjTwo = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
console.log(arrObjTwo);let person = [
{name: "john"},
{name: "jane"},
{name: "imelda"},
{name: "john"},
{name: "jane"}
];
const data = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
console.log(data);let person = [
{name: "john"},
{name: "jane"},
{name: "imelda"},
{name: "john"},
{name: "jane"}
];
const obj = [...new Map(person.map(item => [JSON.stringify(item), item])).values()];
console.log(obj);const addresses = [...]; // Some array I got from async call
const uniqueAddresses = Array.from(new Set(addresses.map(a => a.id)))
.map(id => {
return addresses.find(a => a.id === id)
})var arrayWithDuplicates = [
{"type":"LICENSE", "licenseNum": "12345", state:"NV"},
{"type":"LICENSE", "licenseNum": "A7846", state:"CA"},
{"type":"LICENSE", "licenseNum": "12345", state:"OR"},
{"type":"LICENSE", "licenseNum": "10849", state:"CA"},
{"type":"LICENSE", "licenseNum": "B7037", state:"WA"},
{"type":"LICENSE", "licenseNum": "12345", state:"NM"}
];
function removeDuplicates(originalArray, prop) {
var newArray = [];
var lookupObject = {};
for(var i in originalArray) {
lookupObject[originalArray[i][prop]] = originalArray[i];
}
for(i in lookupObject) {
newArray.push(lookupObject[i]);
}
return newArray;
}
var uniqueArray = removeDuplicates(arrayWithDuplicates, "licenseNum");
console.log("uniqueArray is: " + JSON.stringify(uniqueArray));
Also in JavaScript:
- Title
- datetime to date javascript
- Category
- JavaScript
- Title
- how to make a proxy server node.js
- Category
- JavaScript
- Title
- empty div jquery
- Category
- JavaScript
- Title
- add image in react native
- Category
- JavaScript
- Title
- how to generate random string in javascript
- Category
- JavaScript
- Title
- How to check whether a checkbox is checked in jQuery?
- Category
- JavaScript
- Title
- express get url parameters
- Category
- JavaScript
- Title
- boucle foreach js
- Category
- JavaScript
- Title
- how to map over arrays vuejs
- Category
- JavaScript
- Title
- how to clone an object in javascript
- Category
- JavaScript
- Title
- apache log format json
- Category
- JavaScript
- Title
- convert json to 2d array
- Category
- JavaScript
- Title
- create array of objects javascript
- Category
- JavaScript
- Title
- get current directory vbscript
- Category
- JavaScript
- Title
- event target javascript
- Category
- JavaScript
- Title
- getelementbyid
- Category
- JavaScript
- Title
- iframe reload parent page
- Category
- JavaScript
- Title
- Angularjs datetime
- Category
- JavaScript
- Title
- how to download express without view
- Category
- JavaScript
- Title
- how to export module in node js
- Category
- JavaScript
- Title
- bootstrap modal remove gray background
- Category
- JavaScript
- Title
- express js redirect to url
- Category
- JavaScript
- Title
- get args nodejs
- Category
- JavaScript
- Title
- flip a coin javascript
- Category
- JavaScript
- Title
- how to find remainder in javascript
- Category
- JavaScript
- Title
- Javascript compare two objects
- Category
- JavaScript
- Title
- how to update jquery datatable row cell value
- Category
- JavaScript
- Title
- change text of element js
- Category
- JavaScript
- Title
- how to change image source using javascript
- Category
- JavaScript
- Title
- cb() never called! npm ERR! This is an error with npm itself.
- Category
- JavaScript
- Title
- how to write a program that alerts the first 3 letters of the current day in javascript
- Category
- JavaScript
- Title
- find index of object in array javascript
- Category
- JavaScript
- Title
- clear input field value javascript
- Category
- JavaScript
- Title
- how to get last item in array javascript
- Category
- JavaScript
- Title
- how to align text inside react component
- Category
- JavaScript
- Title
- bootstrap 4 open tab when opening modal
- Category
- JavaScript
- Title
- get start of day javascript
- Category
- JavaScript
- Title
- how to manage a db connection in javascript
- Category
- JavaScript
- Title
- how to create a server in node js
- Category
- JavaScript
- Title
- .push js
- Category
- JavaScript
- Title
- how to print console in javascript
- Category
- JavaScript
- Title
- get value from textbox in vanilla javascript
- Category
- JavaScript
- Title
- dom get all tags
- Category
- JavaScript
- Title
- get utc time javascript
- Category
- JavaScript
- Title
- how to build a string javascript es6
- Category
- JavaScript
- Title
- button style not working react native
- Category
- JavaScript
- Title
- filter advantages in js
- Category
- JavaScript
- Title
- how to remove first character from string in javascript
- Category
- JavaScript
- Title
- allow cross origin node
- Category
- JavaScript
- Title
- how to parse json
- Category
- JavaScript
- Title
- columndefs in datatable not working while setting width jquery
- Category
- JavaScript
- Title
- how to include local image files in javascript object
- Category
- JavaScript
- Title
- how to get textedit on mac without download
- Category
- JavaScript
- Title
- how to add role in discord.js v12
- Category
- JavaScript
- Title
- client side redirect javascript
- Category
- JavaScript
- Title
- find a single element in array of objects javascript
- Category
- JavaScript
- Title
- creating an object javascript
- Category
- JavaScript
- Title
- drupal 8 node has field
- Category
- JavaScript
- Title
- check row empty array javascript
- Category
- JavaScript
- Title
- how to delete an element from an array in javascript
- Category
- JavaScript
- Title
- ionic (Emitted value instead of an instance of Error
- Category
- JavaScript
- Title
- ion-fab-button ionic angular
- Category
- JavaScript
- Title
- get a div text content and store in a variable js
- Category
- JavaScript
- Title
- async await promise all javascript
- Category
- JavaScript
- Title
- angular 6 reload current page
- Category
- JavaScript
- Title
- D3.js
- Category
- JavaScript
- Title
- chartjs lineTension
- Category
- JavaScript
- Title
- how to remove session data on tab close in redux\
- Category
- JavaScript
- Title
- how to get the size of the window in javascript
- Category
- JavaScript
- Title
- javascript array pop
- Category
- JavaScript
- Title
- in express how do you set the location header
- Category
- JavaScript
- Title
- array vowels js
- Category
- JavaScript
- Title
- change select value jquery
- Category
- JavaScript
- Title
- get url of page in background script
- Category
- JavaScript
- Title
- how to find the index of a value in an array in javascript
- Category
- JavaScript
- Title
- how to convert time to am pm in javascript
- Category
- JavaScript
- Title
- empty a select input using js
- Category
- JavaScript
- Title
- how to remove item from array javascript
- Category
- JavaScript
- Title
- async await javascript push
- Category
- JavaScript
- Title
- deploy angular app on google app engine
- Category
- JavaScript
- Title
- componentWillUnmount functional component for asynchronous requests
- Category
- JavaScript
- Title
- data-widget="pushmenu" collable automatically
- Category
- JavaScript
- Title
- iso to date javascript
- Category
- JavaScript
- Title
- how to generate a random number between 1 and 6 in javascript
- Category
- JavaScript
- Title
- create array with specific size javascript
- Category
- JavaScript
- Title
- express and node
- Category
- JavaScript
- Title
- flowjs attributes
- Category
- JavaScript
- Title
- execute js with innerhtml
- Category
- JavaScript
- Title
- how to render different pages in nodes
- Category
- JavaScript
- Title
- javascript Check if an element is a descendant of another
- Category
- JavaScript
- Title
- if (req.url === "/script.js")
- Category
- JavaScript
- Title
- comment in jsp file
- Category
- JavaScript
- Title
- how to remove an object from array in react native
- Category
- JavaScript
- Title
- add char in specific index stirng javascript
- Category
- JavaScript
- Title
- javascript closure stack overflow
- Category
- JavaScript
- Title
- how to add multiple videos in html5 with javascript
- Category
- JavaScript
- Title
- angular material theme colors
- Category
- JavaScript
- Title
- html get elements by class
- Category
- JavaScript
- Title
- javascript append to object
- Category
- JavaScript
- Title
- angular two way property binding
- Category
- JavaScript