Transform an array of objects into a single string and add it back into the original array

The format of my Array is as follows:-

questionValue = {
  others: [{
      inputValue: [{
        FormTypeValueArrayValue: [
          {
            form_type_value: "a"
          }, {
            form_type_value: "b"
          }, {
            form_type_value: "c"
          }, {
            form_type_value: "d"
          }
        ]
      }]

    },
    {
      inputValue: [{
        FormTypeValueArrayValue: [
          {
            form_type_value: "a"
          }, {
            form_type_value: "b"
          }, {
            form_type_value: "c"
          }, {
            form_type_value: "d"
          }
        ]
      }]
    }
  ]
}

However, the required format should be like this: -

questionLevel = {
  others: [{
    question: "",
    form_type_value: "a,b,c"
  },
  {
    question: "",
    form_type_value: "d,e,f"
  }],
}

I have an array that repeats, so in each array I need the form_type_value as a simple string but within the corresponding array.

Answer №1

questionLevel.others = questionLevel.others.map((other)=>{
   other.form_type_value = other.formTypeValuesForEachQuestion
     .map( value => value.form_type_value).join(',');
   delete other.formTypeValuesForEachQuestion;
   return other;
});

Give this a shot!

Answer №2

Here is a possible solution for your issue: Keep in mind that even though you are avoiding explicit loops, there may still be loops within the function calls at a language level which might not be visible to you. These types of solutions are typically referred to as functional programming.

var questionValue = {
  others: [{
      inputValue: [{
        FormTypeValueArrayValue: [
          {
            form_type_value: "a"
          }, {
            form_type_value: "b"
          }, {
            form_type_value: "c"
          }, {
            form_type_value: "d"
          }
        ]
      }]

    },
    {
      inputValue: [{
        FormTypeValueArrayValue: [
          {
            form_type_value: "a"
          }, {
            form_type_value: "b"
          }, {
            form_type_value: "c"
          }, {
            form_type_value: "d"
          }
        ]
      }]
    }
  ]
}


var questionLevel = {};
questionLevel.others = questionValue.others.map((v) => {
                return { question: "", form_type_value: v.inputValue.map(innerValue => 
                innerValue.
                FormTypeValueArrayValue.map
                (obj => obj.form_type_value).join(','))};
});

console.log(questionLevel);

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 is the best way to restrict string patterns in TypeScript?

I have a type definition that looks like this: type ActionType = 'TypeA' | 'TypeB' | 'TypeC' | 'TypeD'; I need myActionType to be a string that is either one of the ActionTypes or a combination of ActionTypes separa ...

Looking to relocate the Mat expansion indicator?

Has anyone found a way to relocate the mat expansion indicator (Angular, Material Design) so it appears before the title? I couldn't find any information on this in the official material documentation here https://i.sstatic.net/qTOKn.png Appreciate ...

Apply dynamic directives to an HTML string fetched from the server

Receiving a string of HTML from the server with custom directive attributes that Angular should render, but struggling to make the directives work. Is it possible to load HTML containing custom directives? I've been using DomSanitizer.bypassSecurityT ...

Guide on exporting Excel data using Angular and TypeScript

My project involves managing a table of information that includes fields for FirstName, LastName, PhoneNumber, Age, and Date. I've created a function that allows me to export the data to an Excel file, but I only want to export specific fields like Fi ...

The solution to resolving setState not updating within a react context

I am encountering a problem where my context does not seem to update when I attempt to modify it using a react hook. The code snippet is provided below, and I have a feeling that I might be overlooking something minor. appContext.tsx import React, { use ...

The reducer within ngrx/store fails to trigger

In my project using combineReducers with "@angular/core": "4.4.3" and "@ngrx/store": "4.0.3", I am facing an issue where the reducers are not being detected after dispatching the actions. It could be due to my lack of experience with ngrx/store. You can ...

Issue with Sending Messages using SignalR in .NET and Angular

I am attempting to make a simple SignalR example work, following Microsoft's tutorial and using the Weatherforecast .NET/Angular SPA from Visual Studio as a starting point for a project I plan to integrate SignalR with in the future. You can find the ...

Utilize Angular 4 to dynamically load templates within a single component

My task is to create a component with multiple HTML templates, each containing at least 20 controls. Depending on certain conditions, a specific template should be loaded. Note: I have opted for 3 different templates because the controls vary based on the ...

In the latest version of Expo SDK 37, the update to [email protected] has caused a malfunction in the onShouldStartLoadWithRequest feature when handling unknown deeplinks

After updating to Expo SDK 37, my original code that was previously functioning started encountering issues with <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e7c6b6f6d7a23606f7a67786b23796b6c78667f79627a">[email prot ...

Why is the getElement().getProperty("value") function not functioning properly?

I am facing an issue with reading a property in my web component. I am puzzled as to why it is not working correctly. I created a simple example, and after clicking the button, I expect to retrieve the value of the property, but it returns null. I am unsur ...

Align watermark content to the far left side

Having trouble getting my watermark to align properly on the left side of my website's main content. Here is how it currently looks: https://i.sstatic.net/Nfhh5.png The issue arises when I resize the screen or switch to mobile view, as the watermark ...

The issue arises due to conflicting indent configurations between eslint and @typescript-eslint/indent

Currently, I am using eslint and prettier in a TS express application. I am trying to set the tab width to 4, but it appears that there is a conflict between the base eslint configuration and the typescript eslint. When looking at the same line, this is w ...

The altered closure variable ts remains undetectable

Check out the live demonstration I made changes to the flag variable, but TypeScript did not recognize it. Could this be a coding issue? function fn () { let flag = true function f () { // alter the value of flag flag = false } for (let ...

During operation, zxing/ngx-scanner functions properly on Safari but encounters issues on Chrome

Encountering an Issue: Despite implementing HTTPS, Chrome is encountering difficulties when attempting to open the zxing/ngx-scanner, resulting in the following error message: @zxing/ngx-scanner Error when asking for permission. DOMException: Permission de ...

Tips for adding a border to a table cell without disrupting the overall structure of the table

Looking for a way to dynamically apply borders to cells in my ng table without messing up the alignment. I've tried adjusting cell width, but what I really want is to keep the cells' sizes intact while adding an inner border. Here's the sim ...

How can a button click function be triggered in another component?

These are the three components in my dashboard.html <top-nav></top-nav> <sidebar-cmp></sidebar-cmp> <section class="main-container" [ngClass]="{sidebarPushRight: isActive}"> <router-outlet></router-outlet> & ...

Obtain a union type in TypeScript based on the values of a field within another union type

Is it feasible in Typescript to derive a union type from the values of a field within another union type? type MyUnionType = | { foo: 'a', bar: 1 } | { foo: 'b', bar: 2 } | { foo: 'c', bar: 3 } // Is there an automati ...

Publish the current namespace as a new module

Imagine I have a module where I define a namespace with various properties. Here's an example: declare module "database" { export namespace Database { namespace statics { type static1 = any; type static2 = any; } } const dat ...

A step-by-step guide on customizing the background color of a Dialog in Angular Material (Version 16)

I've been attempting to modify the background color of my Angular Material Dialog by utilizing the panelClass property in the MatDialogConfig. Unfortunately, I'm encountering a partial success. I am aiming to set the background color as red (jus ...

Leveraging TypeScript to share information between directives in AngularJS through asynchronous calls

Although I've found some scattered information on how to tackle this issue, I haven't been able to find a solid solution. In my AngularJS application, I have an asynchronous call that fetches data from a server and I need to store it in a variab ...