

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to filter out common array in array of arrays in JavaScript
<p>Suppose we have an array of arrays like this −</p><pre class="result notranslate">const arr = [ [ "Serta", "Black Friday" ], [ "Serta", "Black Friday" ], [ "Simmons", "Black Friday" ], [ "Simmons", "Black Friday" ], [ "Simmons", "Black Friday" ], [ "Simmons", "Black Friday" ] ];</pre><p>We are required to write a JavaScript function that takes in one such array. And the function should return a new array that contains all the unique subarrays from the original array.</p><p>The code for this will be −</p><pre class="prettyprint notranslate">const arr = [ [ "Serta", "Black Friday" ], [ "Serta", "Black Friday" ], [ "Simmons", "Black Friday" ], [ "Simmons", "Black Friday" ], [ "Simmons", "Black Friday" ], [ "Simmons", "Black Friday" ] ]; const filterCommon = arr => { const map = Object.create(null); let res = []; res = arr.filter(el => { const str = JSON.stringify(el); const bool = !map[str]; map[str] = true; return bool; }); return res; }; console.log(filterCommon(arr));</pre><h2>Output</h2><p>The output in the console −</p><pre class="result notranslate">[ [ 'Serta', 'Black Friday' ], [ 'Simmons', 'Black Friday' ] ]</pre>
- Related Questions & Answers
- JavaScript: How to filter out Non-Unique Values from an Array?
- Array of objects to array of arrays in JavaScript
- Filter JavaScript array of objects with another array
- Filter array with filter() and includes() in JavaScript
- Filter array based on another array in JavaScript
- Filter one array with another array - JavaScript
- Add property to common items in array and array of objects - JavaScript?
- Python - Filter out integers from float numpy array
- How to filter an array from all elements of another array – JavaScript?
- JavaScript in filter an associative array with another array
- Partial sum in array of arrays JavaScript
- Convert array of arrays to array of objects grouped together JavaScript
- Filter null from an array in JavaScript?
- How to get the most common values in array: JavaScript ?
- Get the smallest array from an array of arrays in JavaScript
Advertisements