Utilizing javascript to reverse an array and seamlessly filling in the missing elements

Consider an array containing data with increasing percentage values and some missing entries.

For instance:

{
    "months": 11,
    "factor": 1.31,
    "upperMonths": 10.5,
    "lowerMonths": 11.49,
    "limit": 20,
    "percentage": 8
  },
  {
    "months": 10,
    "factor": 1.3,
    "upperMonths": 9.5,
    "lowerMonths": 10.49,
    "limit": 20,
    "percentage": 9
  },
  {
    "months": 8,
    "factor": 1.28,
    "upperMonths": 7.5,
    "lowerMonths": 8.49,
    "limit": 20,
    "percentage": 10
  },
  {
    "months": 7,
    "factor": 1.27,
    "upperMonths": 6.5,
    "lowerMonths": 7.49,
    "limit": 20,
    "percentage": 12
  }

Note that percentage 11 is missing...

We have a function that loops through the array and fills in the missing percentages by duplicating the data from the object above, so percentage 11 will now hold the same data as percentage 10.

You can view the function here: http://jsfiddle.net/guideveloper/xnaqtq9y/11/

Our new challenge is to reverse the array and repeat the process. Now, percentage 12 will become the first object, and then the gaps will be filled in reverse order, resulting in an array like this:

{
    "months": 7,
    "factor": 1.27,
    "upperMonths": 6.5,
    "lowerMonths": 7.49,
    "limit": 20,
    "percentage": 12
  },
  {
    "months": 7,
    "factor": 1.27,
    "upperMonths": 6.5,
    "lowerMonths": 7.49,
    "limit": 20,
    "percentage": 11
  },
  {
    "months": 8,
    "factor": 1.28,
    "upperMonths": 7.5,
    "lowerMonths": 8.49,
    "limit": 20,
    "percentage": 10
  },
  {
    "months": 10,
    "factor": 1.3,
    "upperMonths": 9.5,
    "lowerMonths": 10.49,
    "limit": 20,
    "percentage": 9
  },
  {
    "months": 11,
    "factor": 1.31,
    "upperMonths": 10.5,
    "lowerMonths": 11.49,
    "limit": 20,
    "percentage": 8
  }

The array will then need to be reversed back again.

Any thoughts on how to achieve this?

Answer №1

To achieve the desired outcome, simply reverse the array two times by utilizing the reverse method:


test.reverse()

Answer №3

After extensive testing and adjustments, we have successfully achieved the desired outcome. For those interested, you can view the updated fiddle here:

http://jsfiddle.net/guideveloper/whasgrfL/7/

function updateArray(arr) {
    var index = 1;
    while (index < array.length) {
        if ((array[index]["value"] * 1 - array[index - 1]["value"] * 1) > 1) {
            array.splice(index, 0, { 
              "value": array[index-1].value+1,
              "property": array[index]["property"],
              "info": array[index]["info"],
              "category": array[index]["category"],
              "status": array[index]["status"] });
        }
        index++;
    }
    return array;
}

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

Is the MUI Drawer Open 20% of the Time?

One issue I'm facing is with my MUI drawer - it's supposed to close after a menu item is clicked, but sometimes, about 1 out of 5 times, it remains open. I have based my current code on a helpful post on Stack Overflow. Take a look at the code s ...

Is there a way to incorporate multiple functions into a single sx property, such as color, zIndex, backgroundColor, etc? Can this be achieved in any way?

I am currently developing a single search component that will be used across various sections of my application. Each component receives a prop called search: string to determine its type and apply specific styles accordingly. Although I could use classNam ...

TabContainer - streamline your selection process with inline tabs

I am currently working on a GUI that includes a TabContainer with two tabs, each containing a different datagrid. I initially created the tabcontainer divs and datagrids declaratively in HTML for simplicity, but I am open to changing this approach if it wo ...

The Stepper StepIconComponent prop in MUI is experiencing issues when trying to render styles from the styles object, leading to crashes in the app

Struggling to find a way to apply the styles for the stepper component without using inline styles. I attempted to replicate Material UI's demo, but encountered an error. The code from Material UI's demo that I want to mimic is shown below: http ...

Vue - Syntax error: Unexpected token, expecting "}."

I recently started working with Vue and have encountered a simple component issue. Sometimes, when I run npm run serve or save a file that is already being served, I receive the following error message: E:\Development\website\app>npm run ...

Multi-Slide AngularJS Carousel

My current setup includes a carousel like so: <div> <carousel id="myC" interval="3000" > <slide ng-repeat="order in orders"> <img ng-src="whatever.jpg" style="margin:auto;"> <div ...

What is the best way to retrieve the value of a JavaScript variable and display it within an HTML label tag

In my JavaScript code, I'm attempting to retrieve location coordinates using the onchange event and then display those coordinates in a label. I've tried alerting the coordinates successfully, but I'm struggling to update the label using doc ...

Retrieving the ID and value of a specific dropdown list using jQuery from an array of dropdown lists

My HTML structure looks like this: <?php foreach($active_brand as $brand) { ?> <select name="selector" id="selector"> <?php foreach($options as $option) { ?> <option <?php if($brand['State'] == $option) { ?& ...

What is the method for assigning a string to module variable definitions?

As someone new to TypeScript and MVC, I find myself unsure if I am even asking the right questions. I have multiple TypeScript files with identical functionality that are used across various search screens. My goal is to consolidate these into a single fil ...

Utilizing Various Styles within a single Google Sheets Cell

In Cell A1, I have a challenge where I need to merge 4 different arrays of names, separated by commas. Each name should have its own styling based on the array it belongs to - red for array1, green for array2, orange for array3, and gray with a strike-th ...

Efficient method for identifying the positions of non-zero elements in a 2-dimensional array using Python

In questioning the efficiency of the numpy.nonzero function, I ran a test to compare its performance under different conditions. Here is an example: a=np.random.random((1000,1000)) a[a<0.5]=0 timeit.timeit(lambda:np.nonzero(a),number=100)/100.0 #gives ...

Explore the flexibility of npm by installing and utilizing various versions of a specific package

Is it possible to install and utilize different versions of packages in npm? Installation can be done as follows: npm install -g <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d6d7c7e767c7a78737c70785d2f3325">[email p ...

Issue encountered while submitting form on JQuery Mobile framework

Currently, I am in the process of developing a small web application utilizing JQuery Mobile and Multi Page architecture. The issue arises on my form as the second page. Upon clicking the submit button, an error message is displayed on my console (error i ...

There seems to be an issue with the audioElement.src, which I used to run and play different songs. However, it is now displaying a 404

Array.from(document.getElementsByClassName('songItemPlay')).forEach(function (item) { item.addEventListener('click', (evt) => { playAllSongs(); position = parseInt(evt.target.id); evt.target.classList.re ...

Is it acceptable to include the bundled main.js file in the gitignore for a HUGO project?

Is it possible to exclude the bundled main.js file from a HUGO project by adding it to .gitignore? ...

Is there a way to integrate the javascript and jQuery functions in order to conceal a button?

I recently acquired the File Upload script known as Simple Photo Manager and I am looking to incorporate jQuery functions with the existing JS code in the script. My main goal is to make the Delete button disappear once it has been clicked. However, I am ...

Tips for manipulating rows in HTML using jQuery when the id attribute is added

Apologies in advance for any language errors in my explanation I am working on an input table where each row has a unique ID, and the input in each row affects the next row. As new rows are added, I have implemented an incremental numbering system for the ...

Update a table in Laravel view using AJAX with a table join operation

How can I access the "nombreSubdireccion" attribute from the "subdireccion" table when inserting/updating a new record in the "area" table using AJAX? Currently, I am only able to get it by reloading the page due to DB::table. I'm unsure of where to d ...

Postman Guide: Mastering the Evaluation of JSON Arrays

One of the features in Postman allows users to save a specific field from the response body as a variable and then utilize that variable in a subsequent call. For instance, after my initial call to the web service, the response body contains: [ { "id" ...

Encountering an issue when trying to submit the form - "Cannot POST /public/register-controller

I am currently working on developing a login/signup system using Express.js, MySQL, and Node.js. However, I encountered an error when I clicked on the submit button: Cannot POST /public/register-controller This is my index.html: <!DOCTYPE html> ...