Transfer data from distinct arrays to separate variables

I have 2 arrays structured like this:

let dataA = [{"id": "a1", "name": "Alpha"}, {"id": "a2", "name": "Beta"}, {"id": "a3", "name": "Gamma"}, {"id": "a4", "name": "Delta"}, {"id": "a5", "name": "Omega"}]

and

let dataB = [{"a1": 0, "a2": 100000, "a3": 500000, "a4": 300000, "a5": 45900}]

How can I transfer values from dataB to dataA?

For example, how do I push the value of dataB.a4, which is 300000, to dataA.id == a4?

I would like the final result to be similar to:

let result = `[{"id": "a1", "name": "Alpha", "values": 0}, {"id": "a2", "name": "Beta", , "values": 100000}, {"id": "a3", "name": "Gamma", "values": 500000}, {"id": "a4", "name": "Delta", "values": 300000}, {"id": "a5", "name": "Omega",  "values": 45900}]`

Any assistance would be greatly appreciated as I have been struggling with this for days.

Thank you in advance.

Answer №1

To dynamically add a values key with the value of payrollbalance[0] based on the JSON key, you can utilize the Array.map() method.

let payrollname = [{"code": "a1", "name": "Loan A"}, {"code": "a2", "name": "Loan B"}, {"code": "a3", "name": "Loan C"}, {"code": "a4", "name": "Loan D"}, {"code": "a5", "name": "Loan E"}]

let payrollbalance = [{"a1": 0, "a2": 100000, "a3": 500000, "a4": 300000, "a5": 45900}]

let result = payrollname.map(item => ({
  ...item,
  values: payrollbalance[0][item.code]
}));

console.log(result);

Answer №2

Create a program to search an array, locate the index, and then update it

let payrollname = [{"code": "a1", "name": "Loan A"}, {"code": "a2", "name": "Loan B"}, {"code": "a3", "name": "Loan C"}, {"code": "a4", "name": "Loan D"}, {"code": "a5", "name": "Loan E"}]
let payrollbalance = [{"a1": 0, "a2": 100000, "a3": 500000, "a4": 300000, "a5": 45900}]
var result = [];
let pblance = payrollbalance[0]
for(let p in pblance)
{
  let index = payrollname.findIndex(el => el.code==p)
  let pname = payrollname[index]
  pname.values = pblance[p];
  result.push(pname)
}
console.log(result);

Answer №3

yes, this snippet is sure to deliver!

let employees = [{"id": "001", "name": "John Doe"}, {"id": "002", "name": "Jane Smith"}, {"id": "003", "name": "Michael Johnson"}, {"id": "004", "name": "Emily Brown"}, {"id": "005", "name": "David Lee"}]
let salaries = [{"001": 60000, "002": 75000, "003": 90000, "004": 55000, "005": 80000}]
let payrollResults = []
let index = 0
employees.forEach(person => {
    let payAmount = salaries[0][person.id]
    person.salary = payAmount;
    payrollResults[index] = person
    index=index+1
});
console.log(payrollResults)

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

What could be the reason for my jQuery Bootstrap form not sending properly formatted JSON to the REST service?

I have incorporated Bootstrap and jQuery into my web page. Upon a button click, a modal dialog box appears with a simple form. When submitted, the form content is sent as JSON to my RESTful web service. Take a look at the modal dialog below... <!-- M ...

Having trouble with jQuery div height expansion not functioning properly?

Having a bit of trouble with my jQuery. Trying to make a div element expand in height but can't seem to get it right. Here's the script I'm using: <script> $('.button').click(function(){ $('#footer_container').anim ...

Converting a ReadStream to a ReadableStream in NodeJS: A Step-by-Step Guide

This question poses the opposite scenario to converting a ReadableStream into a ReadStream. ReadStream is a data structure utilized in Node.js ReadableStream is a structure that comes from the web platform, as detailed here As new runtimes like Deno or t ...

Tips for opening a variety of links in new tabs with unique popup buttons

Currently, I am facing a challenge where I need to use a button to trigger an ajax HTML popup and also navigate to a link simultaneously. The issue is that there are multiple buttons on the page, each needing to open different links. Any assistance would b ...

Retrieving pals from the API and showcasing them on the user interface

Currently, I am working on a project involving a unique Chat Application. While progressing with the development, I encountered an issue related to fetching friends data from the backend (node). Even though I can successfully retrieve the friends data in ...

javascript is failing to display the appropriate message at the correct moment

Wrote a code to handle the type of message I should get, looping over all the rows in the table to find matching conditions. These are my conditions: - If all rows have data-mode="success" and data-pure="yes", it's correct and successful. - If any row ...

The Material-UI DataGrid feature allows for the display of column sums, with the sum dynamically updating based on applied filters

I am struggling with calculating the sum of values in the Total Amount column of my Material-UI DataGrid. How can I achieve this and ensure that the sum is also updated when a filter is triggered? Any guidance on how to sum the entire Total Amount column ...

Displaying properties of a class in Typescript using a default getter: Simplified guide

Here is an interface and a class that I am working with: export interface ISample { propA: string; propB: string; } export class Sample { private props = {} as ISample; public get propA(): string { return this.props.propA; } public se ...

What is the process for making an XHR request to a server located remotely

Do you think the code below is sufficient for XHRing a remote server? I tried putting the full server URL in the open method of the XHR object, but it didn't work. I'm not sure if something else needs to be added. <script type="text/javascrip ...

Tips for accessing data directly from a database rather than relying on JSON for every request

After watching a tutorial on YouTube, I was inspired to create a way to deliver articles from a WordPress blog using the JSON API. You can check out the GITHUB link for more details. The tutorial provided a good example, but it only showed data from the d ...

I am experiencing an issue where the result is not appearing on the input tag within my

<script> <form action="do-add-cek.php" id="myForm" method="post" enctype="multipart/form-data"> <div class="form-group"> <label> ...

Node timers exhibiting unexpected behavior contrary to documented specifications

Feeling a bit lost with this one. I'm running Ubuntu and using nvm for node. I made sure to uninstall the version of node installed via apt just to be safe. node --version > v10.10.0 npm --version > 6.4.1 So, I go ahead and create-react-app a ...

The basic jQuery script seems to be malfunctioning

I am trying to attach an on click event to an li element using jQuery. I have written a simple jQuery code within the document ready function, but for some reason it is not functioning as expected. I have checked in both Chrome and Firefox, and there are n ...

Automatically update button appearance upon reaching zero value using JavaScript

Whenever I click the button, the user's HP decreases until it reaches 0 and then the button changes. However, a peculiar issue arises when the userHealth hits zero - the button does not change immediately. An additional click is required for the butto ...

The useEffect hook in ReactJs is triggering multiple times

Encountering challenges while developing an Infinite scroll using ReactJs and Intersection observer API. Upon initial application load, the API gets called twice instead of once. This behavior may be due to strict mode, although not confirmed. Additionall ...

Increase the value of k by using an array insertion loop

Is there a way to increment the value of the variable $k every time a record is inserted in the loop? I need to insert an array with the required values based on the inserted records. Below is a snippet of the code, any help would be appreciated. $k = 0; ...

Find your favorite artist on Spotify through the search function

Recently, I stumbled upon this intriguing demo showcasing how to search for an artist using the Spotify API. However, despite several attempts, I have been unable to make it function properly. Can anyone provide any tips or guidance on making this work suc ...

Using jQuery to select the next table cell vertically

Is there a way to utilize jQuery to access the cell (td) directly below a given cell in a traditional grid-layout HTML table (where each cell spans only one row and column)? I understand that the code below will assign nextCell to the cell immediately to ...

non-concurrent in Node.js and JavaScript

I'm a beginner in the world of NodeJS and I have a question that's been bugging me. Node is known for its asynchronous nature, but JavaScript itself also has asynchronous features (like setTimeout). So why weren't concepts like Promise intr ...

How can I pass an Objective-C object to JavaScript?

Currently, I am working on a UIWebView project that involves HTML and JavaScript. One of my friends is working on something similar but for Android. The challenge I'm facing is that in Android, there is a simple way to send an Android object to JavaS ...