Javascript filter unique / duplicates closure

Date: 2020-08-20
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const filterUnique = () => {
const keys = new Set();
return (key) => !keys.has(key) ? keys.add(key) || true : false;
};
const filterById = filterUnique();
console.log([{ id: 1 },{ id: 2 },{ id: 3 },{ id: 2 }].filter(x => filterById(x.id)));
const filterUnique = () => { const keys = new Set(); return (key) => !keys.has(key) ? keys.add(key) || true : false; }; const filterById = filterUnique(); console.log([{ id: 1 },{ id: 2 },{ id: 3 },{ id: 2 }].filter(x => filterById(x.id)));
const filterUnique = () => {
	const keys = new Set();
	return (key) => !keys.has(key) ? keys.add(key) || true : false;
};

const filterById = filterUnique();
console.log([{ id: 1 },{ id: 2 },{ id: 3 },{ id: 2 }].filter(x => filterById(x.id)));

39180cookie-checkJavascript filter unique / duplicates closure
0 of 2000 max characters.