Comparing Two Items between Two Arrays

I have a scenario where I need to compare values in two arrays by running a condition. The data is pulled from a spreadsheet, and if the condition is met, I want to copy the respective data.

For instance, I aim to compare two cells within a spreadsheet - copying the higher value from one cell and updating the lower value in the second cell. Being new to coding, I would appreciate any advice or guidance on how to approach this.

function copyRANGEHIGH() { 
  var NEWHIGH = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("INDEX-RANGE");
  var OLDHIGH = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("INDEX-RANGE");  

  var VALNEWHIGH1 = NEWHIGH.getRange("U4").getValues(); //DIA
  var VALOLDHIGH1 = OLDHIGH.getRange("P5").getValues(); 

  var VALNEWHIGH2 = NEWHIGH.getRange("U22").getValues();//SPY
  var VALOLDHIGH2 = OLDHIGH.getRange("P23").getValues();

  var VALNEWHIGH3 = NEWHIGH.getRange("U39").getValues();//IWM
  var VALOLDHIGH3 = OLDHIGH.getRange("P40").getValues();

  var arr1=[VALNEWHIGH1,VALNEWHIGH2,VALNEWHIGH3];
  var arr2=[VALOLDHIGH1,VALOLDHIGH2,VALOLDHIGH3];


  for (var i=0; i < arr1.length; i++) {
  for (var j=0; j < arr2.length; j++) {
  //  Browser.msgBox(arr1[i])
     if ( arr1[i] > arr2[j] ) {

Answer №1

Your code suggestion tackles the comparison of two arrays by looping over them and checking if one element is greater than another. Here's an alternative approach:

const newArr=[VALNEW1, VALNEW2, VALNEW3];
const oldArr=[VALOLD1, VALOLD2, VALOLD3];

//By utilizing a single loop that iterates through both arrays simultaneously,
//you can compare corresponding elements efficiently.

for (let i = 0; i < newArr.length; i++) {  
   if (parseFloat(newArr[i]) > parseFloat(oldArr[i])) {
       //Value in newArr is greater
       //Additional actions can be executed here.
   }
}

To enhance the code further and ensure safe numeric conversion, you may consider incorporating additional precautions for unexpected non-numeric values. However, the provided code should offer a solid starting point for your implementation.

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

Issue with loading the main.css file

Getting Started Managing two domains can be a challenge, especially when trying to make them appear as one seamless website. In this case, I have ownership of and . Goal My goal is to merge the content of https://mauricevandorst.com/personal-page/index ...

Regular occurrences of heap memory exhaustion within a node.js application running in a docker container

My node.js application consists of 16 microservices, a docker image, and is hosted on the Google Cloud Platform using Kubernetes. However, when handling API requests from just 100 users, some of the main docker images are crashing due to heap out of memor ...

Classify JavaScript Array Elements based on their Value

Organize array items in JavaScript based on their values If you have a JSON object similar to the following: [ { prNumber: 20000401, text: 'foo' }, { prNumber: 20000402, text: 'bar' }, { prNumber: 2000040 ...

Automatically formatting text upon entering it in Vue.js

I need assistance with auto-formatting the postal code entered by the user. The rule for the postal code is to use the format A0A 0A0 or 12345. If the user inputs a code like L9V0C7, it should automatically reformat to L9V 0C7. However, if the postal code ...

When using NextJS with Redux Persist, the HTML does not get rendered in the initial server

I am currently utilizing ReactJS/NextJS along with Redux and redux-persist, as well as Ant Design. Recently, I have noticed that my html codes and other data are not rendering in the page's source. They do render in the browser and when inspected, but ...

Tips for adding styling to HTML in Vue by entering CSS code into the CodeMirror editor

Is there a way to style HTML by entering CSS code into the Codemirror editor? For instance, if we have the HTML code <head> </head>, how can I apply the CSS code, head{ color : red }, typed in the Codemirror editor to stylize this HTML code? mo ...

Creating visual representations of class, organization, flow, or state diagrams using Vega or Vega-lite

I'm struggling to find an example of a state, class, flow chart, or org chart diagram created with Vega. Are there any available online? Vega seems like the perfect tool for this type of visualization, although it may be a bit complex. Without a star ...

Tips on allowing a rectangle to be draggable using HTML5

I have been experimenting with resizable and draggable rectangles in HTML5. I've managed to create resizable rectangles, but I am having trouble getting them to drag using mouse events. You can view my JSFiddle code at the following link: here. / ...

Creating indexes for a two-dimensional NumPy array

In my pursuit to create a 2D numpy array with elements derived from their positions, I developed the following code snippet: import numpy as np def calculate_element(i, j, other_parameters): # do something return value_at_i_j def main(): arr ...

Executing a personalized function - settings.func does not exist as a valid function

I have developed a custom jQuery plugin where I intend to invoke a specific function like this... (function($) { $.fn.customPlugin= function(options) { var settings = { func: null }; if (options) { $. ...

The Journey of React Components: How Child Components Embrace the State Props of the Past

My understanding of states and props in React seems to be off. I am working on a React, Redux based app. Situation: I have a global SVG component that grabs the viewport dimensions from the app in the componentDidMount method. The default props for the ...

How can I update two divs simultaneously with just one ajax call?

Is there a way to update 2 divs using only one ajax request? The code for updating through ajax is in ajax-update.php. <?php include("config.inc.php"); include("user.inc.php"); $id = $_GET['id']; $item = New item($id); $result = $item->it ...

Allocating arrays on the heap: choosing between separate allocation and contiguous allocation?

Check out this answer for more information: The individual has demonstrated two different methods of allocating arrays on the HEAP: int main(){ const int n = 100000; #ifdef ALLOCATE_SEPERATE double *a1 = (double*)malloc(n * sizeof(double)); ...

Javascript Leap Year Determination using nested if-else statements

I am facing an issue with the nested if statement where all conditions have been provided. Some leap years like 2016 and 2020 are not being recognized as Leap years even though they should be. Can someone please assist me in fixing this error? var y = p ...

The distinction between a keypress event and a click event

Due to my eyesight challenges, I am focusing on keyboard events for this question. When I set up a click event handler for a button like this: $("#button").on("click", function() { alert("clicked"); }); Since using the mouse is not an option for me, ...

Tips for parsing a JSON file within my node.js application?

I am working on a node.js service that involves validating JSON data against a schema defined in jsonSchema. Below is the code snippet for my service: app.post('/*', function (req, res) { var isvalid = require('isvalid'); ...

When using the v-for directive to display all elements of an array, only the information of the clicked array should be listed when using the

I am facing an issue with my array of locations. There are 2 divs in play - one containing the list of locations and the other displaying additional info when a location is clicked. The problem arises when I click on a specific location, let's say Sc ...

Navigating URLs with Ajax Requests in CakePHP

How can ajax calls in javascript files located in the webroot be handled without PHP interpretation? In my project using CakePHP and require.js, I avoid placing javascript directly in views. To address this issue, I set a variable in the layout to store t ...

The accumulation of input using setInterval is not effective

I need some help with my code. Please take a look at this link here. I want the input to count up from zero and hide when I click the "change" button. But when I show the input again, I want its value to reset back to zero. Can anyone guide me on how to ...

Transcribing live content directly from the browser tab

I have a unique idea for developing a browser extension specifically for Chrome. This extension would provide live transcriptions of my team's meetings directly from an open tab in the browser. I am interested in extracting keywords, such as my name, ...