Take the elements of one array and assign them to the elements of another array in typescript, then convert and present the result in JSON

My scenario involves dealing with two arrays, one containing user IDs and the other holding tags. ''' ["ELG001", "ELG002"] ["TG01", "TG02"]

''' I need to send this data to an endpoint that requires a JSON in the following structure ''' "id_tags": [{ "id": "ELG001", "tag": "TG01,TG02" }, { "id": "ELG002", "tag": "TG01,TG02" } ] '''

Is there a way to achieve sending the data in the specified format using the map function?

Answer №1

Here is a customized code snippet I put together for you based on your query. The goal is to assign the first array as IDs and include the second array as tags in your JSON data structure.

const idArray = ["ELG001", "ELG002"];
const tagArray = ["TG01", "TG02"];
let tagArr = [];
idArray.forEach((el) => {
  tagArr.push({
    id: el,
    tag: tagArray.join(",")
  })
})
const tag = {
  id_tags: tagArr
}
console.log(tag);

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

The 'Subscription' type does not contain the properties: source, operator, lift, subscribe, and three other properties that are present in the 'Observable<EnumValue[]>' type

Are you struggling with assigning an Observable<EnumValue[]> to another Observable<EnumValue[]>? fetchContactLocationStates() { this.contactLocationStates$ = this.enumValues$ .pipe(takeUntil(this.destroy$)) .subscribe(x => x.fil ...

Adding Custom CSS File to an iFrame in Angular

Currently, I am attempting to include a CSS file in a third-party iframe to modify its styling. My initial idea was to employ the following code snippet: ngOnInit() { ... this.myIframe.nativeElement.document.head.appendChild('style.css') } U ...

What is the best way to create an ESM / CJS module without needing to include '.default' in the imports?

Is it possible to configure my package.json to support different imports for CJS and ESM formats? "main": "dist/cjs/index.js", "module": "dist/esm/index.js", In addition, can I set up my TypeScript compiler to g ...

Is it possible to achieve pagination by simply dragging the scroll bar to the top or bottom position?

Recently, I've been working on implementing a pagination function for a list of items. The pagination currently works well with scrolling events - it automatically moves to the next page when scrolling to the bottom, and to the previous page when scro ...

What strategies can you employ in Angular to reverse the outcome of a guard using canActivate?

As per the Angular documentation regarding canActivate, it appears that you can only utilize canActivate guards to allow navigation to a route if the canActivate function ultimately returns true. Is there any way to specify, "proceed to this route only if ...

Personalized pagination - React data table

I'm looking to create a unique pagination design using react-table that fits my specific requirements: Currently, the default pagination I'm using looks like this: https://i.sstatic.net/xf1sE.png But I want it to resemble this: https://i.ssta ...

The setLanguage function in jsPDF does not support rendering different language characters

I'm currently working with jsPDF in Angular 2 and I'm experiencing an issue where my HTML content is not converting successfully into a PDF when it's written in Hebrew. Other languages seem to work fine, but Hebrew is causing a problem. How ...

Angular Microfrontend with Module Federation captures and processes HTTP requests within the host application

I am looking for a way to capture HTTP requests from all remote sources within my host application so I can reset a timer that tracks user inactivity. All of the applications involved use Angular with Webpack Module Federation. Are there any suggestions on ...

Can parameters with identical union types in a function signature be streamlined to contain only the exact same subtypes using generic types?

// defining a type Combinable with string or number as possible values type Combinable = string | number; // function to check if parameter is a string function isString(param: unknown): param is string { return typeof param === "string"; } /** * Func ...

Oops! There seems to be an issue where the 'title' property is being accessed from an undefined source

I've been struggling with this code block for the past few days, and even when attempting to initialize it as an object, I encounter errors. Here is my restaurantForm.ts file import { Component, OnInit } from '@angular/core'; import {Rest ...

Bootstrap Modal Fails to Display Image Within Its Container

I'm currently working on my e-commerce website, where I'm attempting to implement a feature that allows users to view product images in a modal for a closer look. The product page contains a list of images, and when a user clicks on one, it shoul ...

Verification function in cypress to ensure accurate display of specific timeframes

Looking to create a Cypress method for checking three different rounding cases regarding the hour. An example of the second case I want to include in a general method, if a specific argument is provided: ( cy.contains('00') && cy.contain ...

Angular Material Select Dropdown that emits the entire object item, rather than just the value

When using the Angular Material select dropdown API, only a value is emitted. Both [(ngModel)] and (selectionChange) will only emit a single member, not the entire object's data fields. For example, it will only emit something like food.value = 2, wi ...

How should I format my JSON data to track monthly attendance?

Do you have a material table that needs to be filled day by day? Here's how you can do it: Check out the image example! I've set up a JSON server with data structured like this: { "posts": [ { "empid": 12345, "name": "Dharini I ...

Retrieving the types of all ids from an object

I have a collection as follows: var myData = [{ id: 'a', value: 1}, { id: 'b', value: 2 }, { id: 'c', value: 3}] and I am trying to create a function that only accepts certain ids as parameters function checkId(id: 'a&ap ...

How to pass a single property as a prop in TypeScript when working with React

I have a main component with a parent-child relationship and I am looking for a way to pass only the product name property as props to my "Title" component. This way, I can avoid having to iterate through the information in my child component. To better i ...

What is the significance of the double exclamation mark operator in determining the truthiness or falsiness of an object's values?

Trying to determine the truthiness or falsiness of an object is proving to be a challenge for me. If an object contains all truthy values, I want one outcome; if it contains falsy values such as 0, an empty string, or undefined, I want a different outcom ...

Violation of return statement protocol discovered in Angular Service

Currently, I am developing an Angular sample application to enhance my skills. Unfortunately, I encountered an error message stating "Illegal return statement" when trying to retrieve the object list from service.ts. Below is snippet of the service method: ...

Printing the default value set in the constructor using HTML

I'm encountering an issue while trying to set a default value from the constructor parameters and display it in HTML. The error message I'm receiving is: No suitable injection token for parameter 'name' of class 'AppComponent' ...

Tips for creating a Playwright method that waits for an item to change its color

Seeking help with a Playwright issue related to waiting for an icon color change. My goal is to create a method using Playwright that will wait until the color of the icon changes to #8E8E90, but it seems to be not working as expected. How can I make this ...