Change from arbitrary sequence to structured array arrangement

Is there a way to rearrange the items in an array according to their position? Here's the code I have:

setColor(colors[Math.floor(Math.random() * colors.length)]);

Instead of choosing randomly, I would prefer to sort the items based on their array position. Thank you!

Answer №1

Check out the current color in use and swap it for the other one.

const [currentColor, setCurrentColor] = useState(false); //or optionally useState("#ff6700")

function onClick() { 
    const availableColors = ["#ff6700", "#ffffff"];

    if (availableColors.indexOf(currentColor) === 0) {
        setCurrentColor(availableColors[1]); 
    } else {
        setCurrentColor(availableColors[0]); 
    }
}

I hope this solution works for you :)

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

The steps to building a personalized checkbox that includes an indeterminate state

I've been attempting to achieve an indeterminate state for a custom checkbox without success. My efforts using css have not yielded the desired outcome. This is what I've tried so far: $(document).ready(function() { var checkboxes = docume ...

When executing the app.delete function, the req.body is found to be empty

I've encountered an issue when trying to send JSON data in an $http Delete call, as the req.body returned is coming back as an empty JavaScript object. Below is my $http delete call where "scenario" is a json object: //Deletes the item from the data ...

What is the process for loading a .x model in Three.js?

Could anyone provide guidance on how to successfully load an animated .x model in Three.js? I am currently struggling with the process and have attempted to convert the .x model to collada without success. Unfortunately, my search for a free program to c ...

What is the best way to utilize an AngularJS directive to send a POST request with all parameters from a form?

I have created a custom AngularJS form with Stripe integration using a directive. You can view the Fiddle for this project here: https://jsfiddle.net/u5h1uece/. HTML: <body ng-app="angularjs-starter"> <script src="https://js.stripe.com/v3/"> ...

Ways to dynamically update a div with data from a JSON response

I'm currently in the process of developing a search platform. I have three static divs on the search results page that display certain content, all containing similar code. For example: <div id="result" class="card"> <img src="hello.png" ...

Navigating a collection of objects after a button is clicked

My current library project involves looping through an array of objects to display them on a grid based on input values. Here is the code snippet for my loop: const addBook = (ev) => { ev.preventDefault(); let myLibrary = []; let bookIn ...

Displaying 'N/A' in the chart if the data is missing

I have a chart that displays data, but when data does not exist it shows "undefined%". https://i.sstatic.net/Fm3Tl.png Is there a way to remove the "undefined%" and simply display nothing on the graph if no data exists? Here is the code snippet: import { ...

What is the best way to use AJAX to load a PHP file as a part

I'm exploring different methods for making an AJAX call with an included file. Let's create a simple working example. Initially, I have my main index.php file which contains the following content. In this file, I aim to access all the data retur ...

Calculating the total sum of values from keys, post applying the filterBy method

HTML: <div id="app"> <h1>{{title}}</h1> <form id="search"> Filter <input name="query" v-model="filterQuery"> </form> <table> <tr v-for="client in clients | filterBy filterQuery"> <td ...

Troubleshooting Problems with File Upload Response in ASP.Net MVC Ajax.BeginForm

I have implemented a modal feature that allows users to upload a file. I am looking for a JSON response to indicate whether the upload was successful or not, and then display this information to the end user. Here is my current View: @model int <div ...

What is the method for altering the background color of an HTML table cell when a particular event occurs?

As I create an html table, I am looking to dynamically change the background color of specific boxes after running a selenium webdriver. For instance, if the webdriver successfully navigates the site, I want the corresponding box in the table to turn gre ...

Assign the JSON data to a variable before passing it

Consider the following code snippet: yAxis : { title : { text : 'Exchange rate' }, plotLines : [{ value : 0.6738, color : 'green', dashStyle : 'shortdash', width : 2, ...

Arranging sets of x, y, and z coordinates into arrays inside a specified space using Python and numpy

Hello, I am a newcomer seeking some guidance on solving a particular problem. I have a set of x, y, z coordinates that I wish to divide into arrays based on points within a specific volume (dV) of the overall space. My approach involves sorting each coord ...

Issue with jSignature transitioning properly from display:none to display:block within a multi-page form

After searching through numerous resources, I have been unable to find a solution to my specific issue. I have a multi-page form that incorporates jSignature as the final tab. The structure closely follows the example from the W3Schools website, tailored t ...

Merge two objects while removing a specific property

Imagine I am with an array of objects in this format "err": [ { "chk" : true, "name": "test" }, { "chk" :true "post": "test" } ] Is there a way to organize it like this instead: "err": [ { "pos ...

add multiple arrays to a new empty array using PHP

I am trying to create a function that will retrieve the children of a parent. The function get_children is intended to return an array of children with the ID $this_parent_id. /* $pages array */ Array ( [0] => Pages Object ( [i ...

Simple integration of JSP, JSON, and AJAX

I need help testing a simple login functionality using AJAX. Here's the code snippet: <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login Test Page</title> <script src="../js/j ...

Draggable element with a centered margin of 0 auto

Encountering an issue with jQuery UI sortable/draggable where the element being dragged, when centered using margin:0 auto, starts dragging from the left side of the container instead of the center where it is positioned. Check out this demo to see the pr ...

Is it achievable to dynamically generate new pages on initial load in NextJS?

Right now, I am utilizing getStaticProps and getStaticPaths to pre-render a specific number of articles before my website goes live. This method works effectively, but if a new article is created and displayed on the front page while the site is still acti ...

C# error: System.Array does not have a method called 'Any'

Working with Visual Basic // C# I am currently attempting to search through my stored arrays in order to find a match for the user's input. For instance, if the user has saved information about a USB device and now wants to retrieve that data. FULL ...