Iterate over elements within an array at a specific position

I'm in the process of developing a JavaScript function that takes a value passed from a servlet, then searches an array to find the index of that value. The loop will then start at that index and continue until it reaches the last value of the array, at which point it will restart from the beginning of the array. Below is the code for my function:

function Calculate(servletValue){
     var calculateArray = [0.000001, 0.000003, 0.00001, 0.00003, 0.0001, 0.0003, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3];
     var index = calculateArray.indexOf(servletValue);
     for(i = 0; i<calculateArray.length; i++){
     console.log(calculateArray[i]);
   }
}

Thank you in advance for any assistance!

Answer №1

Here is a possible code snippet that accomplishes the task:

const reorganizeArray = (value, array) => {
  const index = array.indexOf(value);
  const pivot = index < 0 ? 0 : index;
  return [...array.slice(pivot), ...array.slice(0, pivot)];
}

console.log(
  reorganizeArray(0.003, [0.000001, 0.000003, 0.00001, 0.00003, 0.0001, 0.0003, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3])
)

If the value is not found in the array, the function acts as a no-op. Otherwise, it returns a new array by slicing the original one from the index to the end and then from the beginning to the index.

Answer №2

To simplify the process, I suggest creating a fresh array and iterating through it. Here's an example:

function Process(inputValue){
     var newArr = [0.000001, 0.000003, 0.00001, 0.00003, 0.0001, 0.0003, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3];
     var index = newArr.indexOf(inputValue);
     const myArray = [...newArr.slice(index, newArr.length), ...newArr.slice(0, index)] // consider using a descriptive name 
     // iterate through myArray here...
   }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Java to save integer and floating-point values in an array while excluding other data types

Hello, all! I'm just starting out with JAVA. Currently, I am in the process of developing a program that incorporates a menu and enables users to choose from various options such as specifying the size of an array and inputting elements into it. Basi ...

Java servlet, Selenium, and JavaScript are a powerful combination of tools that can

I am facing a situation where I need Selenium webdriver to be executed on the client side. On a webpage, I have a form with a Submit button inside it. The action attribute of the form calls a servlet named "servletName". Inside the servlet, the followin ...

Querying Angular/Firestore for a collection document to extract a single field from each document and store them in an array

I am currently querying my collection of documents and attempting to extract only the phone numbers into an array. The goal is to store the phone numbers in an array for use by another function. While the Firebase documentation provides examples using co ...

Having trouble getting Jquery Ajax Post to work properly when using JinJa Templating?

Objective: My goal is simple - to click a button and post information to a database. Problem: Unfortunately, clicking the button doesn't seem to be posting to the database as expected. Setup: I am working with Flask Framework, Jquery, and Jinja Temp ...

Issue with typings in TypeScript is not being resolved

I have integrated this library into my code Library Link I have added typings for it in my project as follows Typings Link I have included it in my .ts file like this import accounting from "accounting"; I can locate the typings under /node_modules ...

What are some strategies for streamlining and enhancing the flexibility of this code snippet?

$(document).ready(function() { $('.watermark').focus(function() { if (this.className == 'watermark') { this.className = ''; this.value = ''; } }); $(' ...

Angular: Design dependent on attributes

Can I customize the styling of a div in accordance with a boolean property called "isActive" on my controller using Angular? <div class="col-md-3" (click)="isActive = !isActive"> <div class="center"> <i class="fa fa-calendar"& ...

I am unable to implement v-bind click within a v-for loop

While working with VueJS framework v-for, I encountered a problem when trying to loop through lists of items. Each item index was supposed to be assigned to a variable, but the v-bind click event wasn't being attached properly inside the v-for element ...

Send the array as part of a JSON object, along with additional variables, to be utilized in an AJAX

Today is my first attempt at passing an array to ajax using a JSON object. I'm encountering some difficulties in properly passing an array to a JSON object for ajax consumption. The issue arises when the items variable in the parameter reaches the Con ...

How can I update an image source using JavaScript in a Django project?

Is it possible to dynamically change the image src using onclick without relying on hard paths or Django template tags? I have concerns that this may not be best practice. How can I implement a method to inject/change the ""{% static 'indv_proj&b ...

What is the best way to reach the top of an element that has padding?

An interactive jump menu allows users to navigate to different sections on the page by clicking on menu items. However, there seems to be an issue where the page scrolls to the top of the <h3> element instead of the entire element including any paddi ...

What is the proper way to utilize the setState() function in ReactJS?

I am new to the world of ReactJS and I have been exploring the concepts of state and setState(). While trying to implement a name change using setState(), I found myself wondering where exactly in my code should I invoke the setState() method: Should I c ...

Checking the response from an AJAX call with an if/else statement

Is there a way to create a counter for unread messages using PHP and jQuery? Below is the PHP code in BubbleStat.php: $totalMsg = $mysql->totalRows("SELECT msg_id from messages WHERE msg_opened = 0 AND msg_receiver = '".$_SESSION["ActiveUserSessio ...

Utilizing onBlur and onFocus events in React to implement a dynamic menu that shows or hides

Currently, I am in the process of mastering React and developing a small online electronic store. Within my project, there is a tab labeled "View Store". My goal is for a small, hidden column to appear when a user clicks on this tab, revealing text or imag ...

Guide to using JavaScript to multiply the values from two text fields and showing the result in a separate text field

Here is the code that I am currently using: <script type="text/javascript"> $(function() { $("#addAll2").click(function() { var add = 0; $("#discount") = $dis $(".amt2").each(function() { ...

Padding-left in InfoBox

Currently, I am in the process of developing a map using Google Maps API3 along with the InfoBox extension available at this link I have successfully implemented an overall padding to the Infobox using the following code snippet: var infoOptions = { disa ...

Dealing with Errors and Exceptions in Meteor

As our product prepares to launch, we have users testing it out and encountering random exceptions. Currently, the only way I can be aware of these exceptions is by accessing the server via ssh and sifting through thousands of lines of log files. In my Ja ...

Using a jQuery for loop with two separate methods and distinct indexes

Check out the HTML form below: <form method="post" id="new_message" action="?action=changeRate"> <div class="form-group"> <label for="from" class="col-sm-2 control-label">From</label> ...

Can a Google Charts graphic be designed to feature multiple categories within each column?

Currently, I am considering utilizing Google Charts API with PHP for statistical purposes. The graphics in use are developed using CSS directly to enhance their appearance: https://i.sstatic.net/flLAz.png Upon discovering Google Chart, the idea of recrea ...

The exceljs excel sheet in nodejs is not populating with all the necessary data

In my report task, I need to create an excel file with all the data and then send it to customers via email from the backend. I have successfully used excel.js to write the excel file, but I'm facing issues when dealing with large amounts of data (200 ...