
- 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
JavaScript Return an array that contains all the strings appearing in all the subarrays
We have an array of arrays like this −
const arr = [ ['foo', 'bar', 'hey', 'oi'], ['foo', 'bar', 'hey'], ['foo', 'bar', 'anything'], ['bar', 'anything'] ]
We are required to write a JavaScript function that takes in such array and returns an array that contains all the strings which appears in all the subarrays.
Let's write the code for this function
Example
const arr = [ ['foo', 'bar', 'hey', 'oi'], ['foo', 'bar', 'hey'], ['foo', 'bar', 'anything'], ['bar', 'anything'] ] const commonArray = arr => { return arr.reduce((acc, val, index) => { return acc.filter(el => val.indexOf(el) !== -1); }); }; console.log(commonArray(arr));
Output
The output in the console will be −
['bar']
- Related Questions & Answers
- Return a subarray that contains all the element from the original array that are larger than all the elements on their right in JavaScript
- Finding all the longest strings from an array in JavaScript
- Return an array of all the indices of minimum elements in the array in JavaScript
- Python program to accept the strings which contains all vowels
- All possible odd length subarrays JavaScript
- Sorting an array that contains undefined in JavaScript?
- Check if object contains all keys in JavaScript array
- Print all the pairs that contains the positive and negative values of an element in C++
- Write an algorithm that takes an array and moves all of the zeros to the end JavaScript
- Python - Find all the strings that are substrings to the given list of strings
- JavaScript function that should count all unique items in an array
- Summing all the unique values of an array - JavaScript
- Replace all characters in a string except the ones that exist in an array JavaScript
- Return an array populated with the place values of all the digits of a number in JavaScript
- Sorting an array that contains the value of some weights using JavaScript
Advertisements