Update the Array object and convert it into a new Array object

I am working with a dynamic Array object

this.rating.data = {[4, 1, 8, 3, 3]};

The Array I'm dealing with is

this.rating.labels = ["In", "Lo", "Me", "Hi", "Cri"];

There are cases where some data will be zero

this.rating.data = {[1, 8, 3, 3]};

and

this.rating.labels = ["Lo", "Me", "Hi", "Cri"];

I attempted to handle this by creating a new Array Object, but I encountered difficulties

[{
"In": 0,
"Lo": 1,
"Me": 8,
"Hi" : 3,
"Cri": 3,}]

Answer №1

If you're looking to achieve a specific outcome in JavaScript, consider using the Array.reduce method. Explore more about Array.reduce here

By utilizing reduce, you have the power to manipulate an array into whatever format suits your needs:

let values = [5, 2, 9, 4, 4]
let names = ["A", "B", "C", "D", "E"]
let output = values.reduce((accumulator,currentValue,index)=>{
  accumulator[names[index]] = currentValue; 
  return accumulator
},{})

console.log(output)

Answer №2

While my knowledge of Javascript is limited, one approach you could consider is using a FOR loop to merge the contents of two arrays and then incrementally adding them to your new loop.

I hope this suggestion proves useful, even if it may not be the definitive solution you were seeking.

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

Java AWS Error: Request Entity Exceeds Size Limit

Recently I encountered an issue while trying to upload files to my S3 Bucket after deploying my Spring Boot/Angular application to Elastic Beanstalk. The error message that appeared was 413 (Request Entity Too Large). In an attempt to resolve this, I upl ...

Sending environmental variable values to an Angular application from a Docker file

I am currently facing a challenge with my Angular application where I am attempting to define the environment variable from an external source, specifically from a docker compose file. For reference, I found an article that addresses this issue: docker-c ...

Ways to Halt observable.timer in Angular 2

As I work on Angular2's Component, I am currently implementing the following functions: export class MypageEditComponent { ngOnInit() { this.timer = Observable.timer(100, 100); this.timer.subscribe(t => { this.setFormData(); } ...

Angular data binding with an object instead of an array list

Currently, I am implementing Angular and attempting to iterate through an object. Data in JSON format employee {"fName":"mike","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ebb3b3b3b3b3b3b3b3ab83849f868a8287c588848 ...

Utilizing the Next.js "Link" Element as a Personalized React Component Using Typescript

When attempting to utilize the "Link" element as a custom react component that I modified with typescript to enhance its functionality, I encountered a recurring issue in my project. Each time I used it, I had to include a property named props which contai ...

The release of the Angular App within a webjar is causing problems relating to the baseHref

Currently, I am looking to package my Angular frontend in a webjar so that it can be easily imported via Maven into any Java backend. Successful in this task, I now have a Spring Boot backend with an application.properties file showing: server.servlet.cont ...

The specified type '{ songs: any; }' cannot be assigned to the type 'IntrinsicAttributes' in NEXTJS/Typescript

I am currently working on a straightforward script. Below is the index.tsx file for my Next.js application: import type { NextPage } from 'next' import SongsList from '../components/SongsList/SongsList' import { GetStaticProps } from & ...

Deciding between bundling a Typescript package and using tsc: When is each approach the best choice

When it comes to publishing a Typescript NPM package (library, not client), I have two main options: 1. Leveraging Typescript's compiler First option is to use the Typescript compiler: tsc and configure a tsconfig.json file with an outDir setting: { ...

Is it possible to synchronize the Lit cached DOM with the live DOM?

Utilizing the Lit framework for constructing my front-end UI components has been a game-changer. However, I have encountered an issue while incorporating our internal company design system's web components. One of these components has the ability to r ...

Error message 'Chart was not disposed' is displayed in amChart while rendering live data through a socket connection

I encountered a similar issue with the amChart displaying a "chart was not disposed" warning, leading to memory leakage. To find a solution, I referred to the following guide: However, since I am updating the chart in real-time using socket connections a ...

Exploring the differences in object shapes using TypeScript

I'm currently working on defining an object that has the ability to hold either data or an error. export type ResultContainer = { data: any; } | { error: any; }; function exampleFunction():ResultContainer { return { data: 3 } } ...

Using NestJS to populate data will only populate the first element

I have a Mongoose schema in NestJS structured like this: ... @Prop() casinoAmount: number; @Prop() gameHyperLink: string; @Prop() casinoHyperLink: string; @Prop({ type: Types.ObjectId, ref: 'Game' }) games: Game[]; } I'm t ...

Encountering ng build --prod errors following Angular2 to Angular4 upgrade

Upon completing the upgrade of my Angular2 project to Angular4 by executing the following command: npm install @angular/common@latest @angular/compiler@latest @angular/compiler-cli@latest @angular/core@latest @angular/forms@latest @angular/http@latest @an ...

Using the slice pipe on the data for a child component property is resulting in endless calls to the @Input set method

After incorporating a slice pipe into the data object below and passing that data to the child component's @Input method, there appears to be an endless loop of calls to that method. However, eliminating the slice pipe from the data object resolves th ...

Creating a new endpoint within the Angular2 framework using typescript

I am brand new to Angular2 and I would like to streamline my API endpoints by creating a single class that can be injected into all of my services. What is the most optimal approach for achieving this in Angular2? Should I define an @Injectable class sim ...

Launching Angular 4 front-end and .Net Web Api back-end simultaneously on the same IIS website results in a 404 error when refreshing the page

My web application consists of an Angular 4 front-end client-side code that communicates with a back-end services part written in ASP.NET WebAPI. I have deployed the application on IIS v10, within the same website. However, whenever I try to refresh the pa ...

How can users create on-click buttons to activate zoom in and zoom out features in a Plotly chart?

I am currently working on an Angular application where I need to implement zoom in and zoom out functionality for a Plotly chart. While the default hoverable mode bar provides this feature, it is not suitable for our specific use case. We require user-cr ...

Issue with Angular 2 Custom Pipe not Refreshing Unless Object is Saved

I recently created a custom Angular 2 pipe that formats a phone number from a 10-digit string to 'XXX-XXX-XXXX'. The pipe is functioning perfectly, but the issue arises when it fails to update in real-time during keypress; instead, it updates onl ...

I am able to successfully implement CORS in my .Net 6 and Angular application on my local machine, but unfortunately, the functionality

After struggling for 2 days to make CORS work with my Angular frontend and .Net Core backend, I finally cracked... I set up a basic Angular application with a simple "get" request hitting my backend API. In the backend API, I created a straightforward pro ...

Having trouble with expect().toThrow() function not functioning properly during test execution

I have written a test for a service in Angular4 that checks if an error is thrown when given an invalid Batch ID: it('should throw an error when given an invalid Batch ID', async(inject([EventLogService, HttpTestingController], (servic ...