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 :: scroll up link 
Javascript :: await fetch parameters 
Javascript :: jsx example 
Javascript :: sum of two array in javascript 
Javascript :: convert json to excel in javascript 
Javascript :: textcontent javascript 
Javascript :: javascript for in 
Javascript :: js get selected value by id 
Javascript :: sequelize inner join 
Javascript :: every in javascript 
Javascript :: react native force vertical 
Javascript :: what is getter and setter in javascript 
Javascript :: networkx get nodes 
Javascript :: multiple class to same click jquery 
Javascript :: events onclick 
Javascript :: find string length javascript 
Javascript :: javascript zoom image onclick 
Javascript :: sequelize findall 
Javascript :: jquery get name value method 
Javascript :: remove object if key is duplicate javascript 
Javascript :: cm to inches 
Javascript :: django csrf failed ajax case 
Javascript :: round value down html 
Javascript :: insertadjacenthtml trong js 
Javascript :: pwa cache viewer 
Javascript :: indexof all occurrences javascript 
Javascript :: nextjs link image 
Javascript :: mongooseautoincrement 
Javascript :: expo location background example 
Javascript :: react native swipe screen 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =