Steps to increase a specific value in a two-dimensional array

Seeking the most effective method in JavaScript (ES6) to accomplish this task.

Let's consider a 2D array as shown below:

const two_dimensional_array = [
   [33000, 100],
   [33500, 200],
]

Now, we have another 2D array like this:

const another_array = [
 [33500, 1300]
]

The objective is to locate the first value from another_array within two_dimensional_array and then add the second value to it. As a result, the updated two_dimensional_array would look like this:

const two_dimensional_array = [
   [33000, 100],
   [33500, 1500],
]

Answer №1

  • Generate a Map based on the values in another_array, where the first number of each pair is the key and the second number is the value.
  • Utilize Array#forEach to iterate through two_dimensional_array and check for any additional numbers to add (or default to 0 if not found).

const 
  two_dimensional_array = [ [33000, 100], [33500, 200] ],
  another_array = [ [33500, 1300] ];

const map = new Map(another_array);

two_dimensional_array.forEach(arr => arr[1] += map.get(arr[0]) ?? 0);

console.log(two_dimensional_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

React - group several elements within a wrapping container

I am dealing with an array of words that make up sentences: let words = [ { "start_time": "2.54", "end_time": "3.28", "alternatives": [ { "confidence": "1.0", ...

It is imperative that the 'Access-Control-Allow-Origin' header value in the response is not set to '*' when the request's credentials mode is 'include'

I am currently working on establishing a connection using socket.io between Angular and a Node.js Server Within Angular, I have set up a new socket by importing socket.io-client and connecting it as follows: import * as io from 'socket.io-client& ...

Create a variable in Angular to determine the selected array

I have a JSON array and I am displaying the items in a select element. When an item is clicked, a new select is generated for any subarray items. If you follow this link, you can see exactly what I mean. Everything is working well so far, but I'm ha ...

Issue with bookmarklet compatibility on mobile browsers like iOS and Android

Here is the bookmarklet code I have: javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js";c.onload=c.onreadystate ...

What is the most efficient and hygienic method for storing text content in JavaScript/DOM?

Typically, I encounter version 1 in most cases. However, some of the open source projects I am involved with utilize version 2, and I have also utilized version 3 previously. Does anyone have a more sophisticated solution that is possibly more scalable? V ...

Looking for a Plugin that Can Responsively Display Images in a Vertical Layout - Does One Exist using Jquery

Looking for a gallery slider plugin that is responsive both horizontally and vertically. Have tried Flexslider1/2, Galleria, and others but they do not adjust to vertical resizing. Changing the CSS did not help. If you resize the browser horizontally with ...

JavaScript: Choosing an element by class within the current row using jQuery

One of my tasks involves generating a table with values fetched from a database. The column totals are displayed at the end of the table. I am looking to provide users with the option to remove specific rows they do not wish to see, and have the total auto ...

What steps should I take to verify the validity of an Angular form?

I am struggling with validating an inscription form in HTML. Despite trying to implement validations, the inputs are still being saved in the database even when they are empty. Here are the validations I want to include: All inputs are required Email addr ...

JavaScript will continue to process the submit to the server even after validation has been completed

My current challenge involves implementing form validation using JavaScript. The goal is to prevent the form from being sent to the server if any errors are found. I have made sure that my JavaScript function returns false in case of an error, like so: ...

All hail the powerful nodejs server.listen() function, the gate

My journey into learning about nodejs servers and websockets has just begun. Currently, I am exploring a server written in javascript using socket.io and express. var app = require('express')(), server = require('http').Server(app) ...

ag-Grid - Automatically adjusting the height of detail section when data updates

I have implemented a table with a Master/Detail setup and I am utilizing the getRowHeight callback. Initially, the row height is set correctly. I expect that when the table data is updated, the row height for rows containing a detail grid should be recalc ...

Setting up a nodejs application on a web server

As a newcomer to NodeJS, I am currently learning how to create a sample app using this technology. I have successfully tested it on my localhost, but now I am facing issues when trying to host it on a public server. var http = require('http'); ...

Highlighting a row upon being clicked

How can I implement a feature in my HTML page that highlights a row when selected by changing the row color to #DFDFDF? If another row is selected, I would like the previously selected row to return to its original color and the newly selected row to be h ...

Using a JMeter variable within JavaScript code: Tips and tricks

I am exploring ways to automate the process of logging in multiple users with different credentials on a web page. My solution involves using JMeter with Selenium to achieve this automation. To read the usernames and passwords from a CSV file, I need to pa ...

Ways to prevent images from vanishing when the mouse is swiftly glided over them

Within the code snippet, the icons are represented as images that tend to disappear when the mouse is swiftly moved over them. This issue arises due to the inclusion of a transition property that reduces the brightness of the image on hover. However, when ...

Employ linq for querying through an array of strings

Just starting out with linq and trying to grasp some basic functionality. I have a string that needs to be split into an array, and then I want to search for specific values within that array. This is what I have so far: string input = "a1,b2,c3,d4"; var ...

Tips for creating text that adjusts to the size of a div element

I'm currently working on developing my e-commerce website. Each product is showcased in its own separate div, but I've encountered a challenge. The issue arises when the product description exceeds the limits of the div, causing it to overflow ou ...

Having trouble with the automatic closing feature of Bootstrap drop down in React?

While using the drop button feature of Bootstrap, I have encountered a situation where it works fine when clicked on, but disabling it by clicking outside using autoClose='outside' does not seem to work as expected. When I click on my hamburge ...

NextJS's Server-Side Rendering makes it incompatible with Reactotron

While setting up the redux store for my NextJS application, I usually rely on the Reactotron library to inspect the store. However, since NextJS involves server-side rendering, importing the configuration in the app file results in an error message saying ...

Tips for utilizing the simple-peer module within a Node.js environment?

I recently started using Node.js and I'm interested in incorporating the simple-peer module into my application. However, I am struggling to understand how to implement it based on the provided documentation. Are there any resources available that can ...