
- 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
Find and return array positions of multiple values JavaScript
We have to write a function, say findPositions() that takes in two arrays as argument. And it should return an array of the indices of all the elements of the second array present in the first array.
For example −
If the first array is [‘john’, ‘doe’, ‘chris’, ‘snow’, ‘john’, ‘chris’], And the second array is [‘john’, chris]
Then the output should be −
[0, 2, 4, 5]
Therefore, let’s write the code for this function. We will use a forEach() loop here;
Example
const values = ['michael', 'jordan', 'jackson', 'michael', 'usain', 'jackson', 'bolt', 'jackson']; const queries = ['michael', 'jackson', 'bolt']; const findPositions = (first, second) => { const indicies = []; first.forEach((element, index) => { if(second.includes(element)){ indicies.push(index); }; }); return indicies; }; console.log(findPositions(values, queries));
Output
The output in the console will be −
[ 0, 2, 3, 5, 6, 7 ]
- Related Articles
- How to find elements of JavaScript array by multiple values?
- JavaScript Array: Checking for multiple values
- Remove same values from array containing multiple values JavaScript
- Return indexes of greatest values in an array in JavaScript
- Comparing objects in JavaScript and return array of common keys having common values
- JavaScript: Combine highest key values of multiple arrays into a single array
- Finding all peaks and their positions in an array in JavaScript
- Can a method return multiple values in Java?
- How do we return multiple values in Python?
- How to check whether multiple values exist within a JavaScript array
- Return the fractional and integral parts of array values in Numpy
- Find indexes of multiple minimum value in an array in JavaScript
- Haskell Program to return multiple values from the function
- How to find and return the longest repeating series of numbers in array with JavaScript
- Find unique and biggest string values from an array in JavaScript

Advertisements