- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a value exists in an array and get the next value JavaScript
We are required to write a JavaScript function that takes in an array of literals as the first argument and a search string as the second argument.
The function should the array for the that search string. If that string exists in the array, we should return its next element from the array, otherwise we should return false.
Example
const arr = ["", "comp", "myval", "view", "1"] const getNext = (value, arr) => { const a = [undefined].concat(arr) const p = a.indexOf(value) + 1; return a[p] || false; } console.log(getNext('comp', arr)); console.log(getNext('foo', arr));
Output
And the output in the console will be −
myval false
- Related Articles
- Check if a particular value exists in Java LinkedHashMap
- Check if a particular value exists in Java TreeSet
- Check if a PHP cookie exists and if not set its value
- Check if value exists for a field in a MongoDB document?
- How to check if a value exists in an R data frame or not?
- Check if value exists in a comma separated list in MySQL?
- Check if a value exists in a column in a MySQL table?
- Java Program to check if a particular value exists in TreeMap
- Python - Check if a number and its double exists in an array
- Python - Check if a number and its triple exists in an array
- Java Program to check if a given value exists in a HashMap
- Check if a value is present in an Array in Java
- C# program to check if a value is in an array
- Check whether a value exists in JSON object?
- Method to check if array element contains a false value in JavaScript?

Advertisements