
- 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
Picking all elements whose value is equal to index in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument.
The function should then construct and return a new array based on the original array.
The new array should contain all those elements from the original array whose value was equal to the index they were placed on.
Note that we have to check the value and index using 1-based index and not the traditional 0- based index.
For example −
If the input array is −
const arr = [45, 5, 2, 4, 6, 6, 6];
Then the output should be −
const output = [4, 6];
Example
The code for this will be −
const arr = [45, 5, 2, 4, 6, 6, 6]; const pickSameElements = (arr = []) => { const res = []; const { length } = arr; for(let ind = 0; ind < length; ind++){ const el = arr[ind]; if(el - ind === 1){ res.push(el); }; }; return res; }; console.log(pickSameElements(arr));
Output
And the output in the console will be −
[4, 6]
- Related Articles
- Picking index randomly from array in JavaScript
- Program to find index whose left and right elements sums are equal in Python
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- Picking the largest elements from multidimensional array in JavaScript
- C++ Program to count number of operations needed to place elements whose index is smaller than value
- Picking all the numbers present in a string in JavaScript
- Count all triplets whose sum is equal to a perfect cube in C++
- Python – Check if elements index are equal for list elements
- Python – Check if elements in a specific index are equal for list elements
- Adjacent elements of array whose sum is closest to 0 - JavaScript
- JavaScript to push value in empty index in array
- Minimum operation to make all elements equal in array in C++
- Mask array elements equal to a given value in Numpy
- Find the closest index to given value in JavaScript
- Count pairs from two linked lists whose sum is equal to a given value in C++

Advertisements