
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- 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
- Find All the Subarrays of a Given Array in Java
- All possible odd length subarrays JavaScript
- Check if object contains all keys in JavaScript array
- How to return all matching strings against a RegEx in JavaScript?
- Return an array populated with the place values of all the digits of a number in JavaScript
- Python program to accept the strings which contains all vowels
- JavaScript function that should count all unique items in an array
- How to trim all strings in an array in PHP ?
- Sorting an array that contains undefined in JavaScript?
- From an array of arrays, return an array where each item is the sum of all the items in the corresponding subarray in JavaScript
- Sum of All Possible Odd Length Subarrays in JavaScript
- Replace all characters in a string except the ones that exist in an array JavaScript

Advertisements