Combining certain key values from two dictionaries based on matching IDs in Angular

I am currently working with two arrays of JSON objects in Angular. Both dictionaries have a key-value pair called "song_id" in common. My goal is to combine the "rating" key-value from the second array into the first array where the song_id matches.

Array 1:

this.Array1 = [
  {"song_id": 01, "artist_name": "Arijit, Shreya", "song_name": "Song1"},
  {"song_id": 02,"artist_name": "Jason, Ankit", "song_name": "Song2"},
  {"song_id": 03,"artist_name": "Asha, Lata, Bapi", "song_name": "Song3"},
]

Array 2:

this.Array2 = [
{"song_id": 01, "user_id": 5, "rating": 4},
{"song_id": 02, "user_id": 5, "rating": 5},
{"song_id"" 03, "user_id": 5, "rating": 3},
]

Expected Outcome:

this.Array1 = [
  {"song_id": 01, "artist_name": "Arijit, Shreya", "song_name": "Song1", "rating": 4},
  {"song_id": 02,"artist_name": "Jason, Ankit", "song_name": "Song2", "rating": 5},
  {"song_id"": 03,"artist_name": "Asha, Lata, Bapi", "song_name": "Song3", "rating": 3},
]

Answer №1

Take inspiration from the following code snippet:

let artists = [
  { artist_id: 1, name: 'Arijit, Shreya', song_name: 'Song1' },
  { artist_id: 2, name: 'Jason, Ankit', song_name: 'Song2' },
  { artist_id: 3, name: 'Asha, Lata, Bapi', song_name: 'Song3' },
];
let ratings = [
  { song_id: 1, user_id: 5, rating: 4 },
  { song_id: 2, user_id: 5, rating: 5 },
  { song_id: 3, user_id: 5, rating: 3 },
];

artists.map((artist) => {
  let artistID = artist.artist_id;
  ratings.map((rating) => {
    let songID = rating.song_id;
    if (artistID === songID) {
      artist['rating'] = rating.rating;
      console.log(artist);
    }
  });
});

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

Troubleshooting a Pulumi script in Azure using Typescript and encountering difficulties with function writing

My background is in using terraform, but now I am trying out Pulumi/typescript for the first time. In my codebase, I have two files - index.ts and blob.ts. The create function in blob.ts is responsible for creating a storage account, resource group, blob ...

Printing error stack that includes the source from the source map

I've been trying to take advantage of the native support for source maps in Node, but I'm having trouble getting them to work when printing errors to the console. Despite running node with --enable-source-maps and using the source-map-support pa ...

What is the best way to transfer a value from a parent component to a directive within Angular 2?

In the parent component, I currently have this line of code: <input datepicker type="text" (change)="update($event)"/> Is there a way for me to provide a value to the datepicker directive? ...

Tips for refreshing the 'state' of an Angular Router component

I'm facing an issue with passing data between pages using Angular routing. Everything works as expected when navigating to the "next" page for the first time. However, upon returning to the home page and selecting a different item, the Router's s ...

The name "Identifier" has already been declared before

I am currently working on a social network project to enhance my skills in nodejs and reactjs. While debugging the backend code for /signin using Postman, I encountered an error that prevents me from launching the node server. The error message displayed i ...

Difficulty encountered while managing dropdown functionality in Protractor using TypeScript

I'm encountering some difficulties when it comes to selecting a dropdown in Protractor. Here's the structure of my DOM: https://i.stack.imgur.com/qK8sT.png This is the XPath I'm using to select the dropdown with the value "Yes": //label[ ...

The element of type 'OverridableComponent<LinkTypeMap<{}, "a">>' cannot be assigned to a 'ReactNode'

I'm currently working on a project where there's a component named ListItemTextStyle. Within that component, the prop type is defined as follows: import { LinkProps, ListItemButtonProps, } from '@mui/material'; type IProps = LinkP ...

Tips for extracting the y-coordinate from a touch event using d3

I am utilizing d3.js to display data in my Ionic app. I have a touch event that allows me to move a line and retrieve the coordinates where it intersects with my chart. While I can easily obtain the x-coordinate representing the date, I am struggling to ge ...

Give the Row ID as a parameter to a personalized component within MUI Datagrid Pro

I am currently exploring the idea of incorporating an intermediate state to row checkboxes based on the selection status of other checkboxes within a detailed panel. My approach involves crafting a custom checkbox component and implementing some logical ...

Why does the CSHTML button containing a JavaScript onclick function only function intermittently?

I've implemented a download button on a webpage that dynamically assigns an ID based on the number of questions posted. Below is the code for the button: <input data-bind="attr: { id: $index() }" type="button" value="Downlo ...

Conserving node.js native imports for Electron with rollup

I am working on a project using Electron, Svelte, and Typescript. Initially, I used a specific template from here, but it restricted access to node.js built-in imports like fs for security reasons in the browser/electron frontend. However, I do not requir ...

The Angular 18 component was brought in as a dependency and integrated into the project, but unfortunately, it

How do I properly integrate the NavbarComponent into the template of the app component? https://i.sstatic.net/pBKTzUnf.png app.component.html <app-navbar></app-navbar> <router-outlet></router-outlet> main.ts import {CdkTableModul ...

What is the reason for the manual update of a view when copying an object's attributes into another object, as opposed to using Object.assign()?

In my application, I have a parent component called 'EmployeeComponent' that is responsible for displaying a list of employees. Additionally, there is a child component named 'EmployeeDetailComponent' which displays the details of the s ...

Using useState to initialize a long value

Obtaining a very large state from JSON can be challenging, especially when it consists of at least 50 lines. During page generation, an error like "there is no such value" may occur if the initial value is not set and the interface is not assigned properl ...

Navigate directly to the dashboard page in Angular 5 using RxJS without needing to load the main page first

Utilizing Firebase as the API in my application for authentication, database management, and storage. Implemented a main page (localhost:4200) that loads when the user is not logged in, and a dashboard (localhost:4200/dashboard) for authenticated users. ...

After downloading the quickstart (angularjs2) from angular.io, I encountered an error when trying to start npm

[email protected] typing start G:\quickstart\quickstart-master tsc && concurrently "tsc -w" "lite-server" [1] 'lite-server' is not recognized as a valid command, [1] executable program or batch file. [1] lite-serve ...

Typescript - Iterating through CSV columns with precision

I am currently facing a challenge in TypeScript where I need to read a CSV file column by column. Here is an example of the CSV data: Prefix,Suffix Mr,Jr Mrs,Sr After researching on various Stack Overflow questions and reading through TypeScript document ...

Conditional type in Typescript can be used to infer tuple types

Why are output1 and output2 not both inferred as [number, number, number] in the following code snippets? Snippet 1 : type InferTuple1<T> = T extends any[] ? [...T]: never; const func1 = <T>(t: InferTuple1<T>) => t; const output1 = ...

Ways of invoking a component method from a service in Angular 2

I have a concept for creating a unique service that is capable of interacting with one specific component. In my application, all other components should have the ability to call upon this service and then have it interact with the designated component in ...

Modify the appearance of a specific date using ng-pick-datetime

Currently, I am incorporating the Angular Date Time Picker into my frontend design. On the backend, there is a method that determines all the dates that are unavailable for a particular user. I am looking to adjust the color on the calendar to highlight t ...