Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Remove duplicate items form array using reduce() method.

const names = [
	'shibu',
	'neymar',
	'vinicius',
	'pattinson',
	'ronaldo',
	'shibu',
	'neymar',
	'pattinson',
	'diCaprio'
];

let onlyName = names.reduce((accumulator, currentValue) => {
	if (accumulator.indexOf(currentValue) === -1) {
		accumulator.push(currentValue);
	}
	return accumulator;
}, []);

console.log(onlyName);

//Expected result => ['shibu', 'neymar', 'vinicius', 'pattinson', 'ronaldo', 'diCaprio'
Comment

Remove duplicates from arrays using reduce

var numbers = [1, 1, 2, 3, 4, 4];

function unique(array){
  return array.reduce(function(previous, current) {
     if(!previous.find(function(prevItem){
         return prevItem === current;
     })) {
        previous.push(current);
     }
     return previous;
 }, []);
}

unique(numbers);
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript element read attribute 
Javascript :: Regular expression: Match everything after a particular word 
Javascript :: convert model object to json django 
Javascript :: jquery add multiple attribute to element by class 
Javascript :: convert date to string format dd/mm/yyyy javascript 
Javascript :: Codewars JS Multiples of 3 or 5 
Javascript :: document print from html javascript 
Javascript :: load base64 image in tab javascript 
Javascript :: js read from json1 
Javascript :: how to check if an object is map in javascript 
Javascript :: json with multiple objects 
Javascript :: how to pass sequelize transaction to association helper method 
Javascript :: js get parameters 
Javascript :: add date in javascript 
Javascript :: element ui handle enter key 
Javascript :: export default arrow function 
Javascript :: javascript - get the filename and extension from input type=file 
Javascript :: how to change css style on click 
Javascript :: get status of a user discord js 
Javascript :: typeof array javascript 
Javascript :: javascript strip 
Javascript :: Jquery handle change event of file upload created dynamically 
Javascript :: discord.js on ready 
Javascript :: find space in string js 
Javascript :: local storage ha 
Javascript :: How to write inside a div using javascript 
Javascript :: JavaScript HTML DOM Changing HTML Style 
Javascript :: javascript remove all spaces 
Javascript :: react native text capitalize 
Javascript :: how to preview a pdf document in react 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =