
- 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
Getting elements of an array depending on corresponding values of another JavaScript
Suppose we have two arrays, for an example consider the following two −
const array1 = ['a','b','c','d','e','f','g','h']; const array2 = [ 1, 0, 0, 1 , 0, 0, 1, 0];
Both the arrays are bound to have the same length. We are required to write a function that when provided with an element from the second array, returns a subarray from the first array of the all elements whose index corresponds to the index of the element we took as an argument in the second array.
For example: findSubArray(0) should return −
[‘b’, ‘c’, ‘e’, ‘f’, ‘h’]
Because these are the elements present in the first array on indices 1, 2, 4, 5, 7, at which in the second array 0’s is present.
Therefore, now let’s write the code for this function −
Example
const array1 = ['a','b','c','d','e','f','g','h']; const array2 = [ 1, 0, 0, 1 , 0, 0, 1, 0]; const findSubArray = (first, second, el) => { if(first.length !== second.length){ return false; }; return second.reduce((acc, val, ind) => { if(val === el){ acc.push(first[ind]); }; return acc; }, []); }; console.log(findSubArray(array1, array2, 0)); console.log(findSubArray(array1, array2, 1));
Output
The output in the console will be −
[ 'b', 'c', 'e', 'f', 'h' ] [ 'a', 'd', 'g' ]
- Related Articles
- Constructing an array of smaller elements than the corresponding elements based on input array in JavaScript
- JavaScript example to filter an array depending on multiple checkbox conditions.
- 8086 program to determine modulus of first array elements corresponding to another array elements\n
- How to filter an array from all elements of another array – JavaScript?
- Make an array of another array's duplicate values in JavaScript
- Order an array of words based on another array of words JavaScript
- Creating an array of objects based on another array of objects JavaScript
- Modify an array based on another array JavaScript
- Looping through and getting frequency of all the elements in an array JavaScript
- How to replace elements in array with elements of another array in JavaScript?
- Equality of corresponding elements in JavaScript
- Append the current array with the squares of corresponding elements of the array in JavaScript
- Frequency of elements of one array that appear in another array using JavaScript
- Sort object array based on another array of keys - JavaScript
- Get range of months from array based on another array JavaScript

Advertisements