Using Typescript to extract elements from one array and create a new array

I have a set of elements "inputData" , and it appears as follows :

[{code:"11" , name= "test1" , state:"active" , flag:"stat"},
{code:"145" , name= "test2" , state:"inactive" , flag:"pass"},
{code1:"785" , name= "test3" , state:"active" , flag:"stat"},
...
]

Instead of using loops, I aim to filter it in order to get an outputData array that looks like this :

[{id:"11" , libelle= "test1"},
{id:"145" , libelle= "test2"},
{id:"785" , libelle= "test3"},
...
]

where

code -> id

and

name -> libelle

Any ideas for this?

Answer №1

To transform your array, you can use the map method to create a new array based on the original one.

const newData = oldData.map(({ code: id, name: label }) => ({ id, label }))

It's worth noting that this code utilizes modern ES6 functionalities such as Array.prototype.map, destructuring assignment, and arrow functions.

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

Unlocking the capabilities of Chrome extensions using Angular 2 and TypeScript

Currently attempting to develop a chrome extension using angular2 and typescript, I have hit a roadblock in trying to access the chrome API (in this case, chrome.bookmarks). I have successfully gained access to the chrome object by following guidance from ...

What are the steps to set up NextJS 12.2 with SWC, Jest, Eslint, and Typescript for optimal configuration?

Having trouble resolving an error with Next/Babel in Jest files while using VSCode. Any suggestions on how to fix this? I am currently working with NextJS and SWC, and I have "extends": "next" set in my .eslintrc file. Error message: Parsing error - Can ...

Having difficulty launching a TypeScript Node.js application using ts-node and pm2

I have a node app built with TypeScript and it works fine with nodemon, but when I try to move it to the server using PM2, I encounter errors. I've searched GitHub and StackOverflow for solutions, but nothing has worked for me so far. Any help would b ...

Typescript declaration for a .js file in a diverse project

Hey there! I'm currently in the process of converting my JavaScript React Redux project to TypeScript, and I've decided to kick things off by tackling my redux reducers file. Here's a snapshot of how my project is structured: Project/ .. ...

Encrypt and decrypt your store with ngrx-store-localstorage

I am attempting to encrypt data stored using ngrx-store-localstorage, following the instructions provided in the documentation: import { ActionReducer, MetaReducer } from '@ngrx/store'; import { localStorageSync } from 'ngrx-store-localstor ...

`Firebase User Instance and Custom Firestore Document`

Recently, I posted a question regarding Google Firebase Angular Firestore switchMap and encountered some issues. The question can be found here. After exploring AngularFireAuth, I learned that it is used to create a User object with fixed values, requirin ...

Tips for deploying an Angular 6 application on a Node server

I am working on an Angular 6 application that needs to be served by a Node server running on port 8080. After some research, I found that adding a server.js file with certain configurations can help achieve this. However, I am unsure about how to modify th ...

Tips for sending arguments to translations

I am currently implementing vuejs 3 using TS. I have set up my translation files in TypeScript as shown below: index.ts: export default { 'example': 'example', } To use the translations, I simply do: {{ $t('example') }} N ...

The process of transferring information from a JSON API to TypeScript models

When working with my JSON API in my services, I need to pass the data to my models. What is the most efficient way to accomplish this task? Currently, I am following this process: import { Attachment } from '.'; export class Contact { id: nu ...

Implementing nested popup windows using Bootstrap

I am facing an issue with my two-page sign-in and sign-up setup in the header of my angular2 project. On the sign-up page, I have a link that should redirect to the sign-in page when clicked, but I am struggling to make it work. Can someone provide guidanc ...

Is there a way to include the request body (req.body) in the msg object using express-winston?

My current challenge involves logging {{ req.body }} using the msg: object in express-winston. Even after whitelisting the body with expressWinston.requestWhitelist.push('body');, it still does not appear in the log. export const accessLogger = ...

Issue detected in React Rollup: the specific module 'name' is not being exported from the node_modules directory

Currently in the process of creating a library or package from my component. The tech stack includes React, Typescript, and various other dependencies. Encountering an error while using Rollup to build the package: [!] Error: 'DisplayHint' is ...

The bundle.js file encountered an issue while running UglifyJs, expecting a name

I have been attempting to utilize UglifyJS to compress/minimize my bundle.js file. However, when executing webpack -p, I encountered the following error message: ERROR in bundle.js from UglifyJs Name expected [bundle.js:105519,6] The line causing the iss ...

p-calendar validator necessitated

Currently, there is no built-in validator for the p-calendar control in PrimeNG: <p-calendar formControlName="startDateControl" [minDate]="today" showIcon="true"></p-calendar> Are there any alternative solutions available? ...

When the admin clicks the start button, redirect all users to the designated page

Currently, I am developing a voting application that features both an admin and users. My goal is for the voting campaign to kick off as soon as the admin clicks on the start button. Upon starting the voting process, I aim to redirect all users to a diff ...

Unable to click on element at specific location

Hey there, I'm encountering an issue trying to click on a specific element and it's not working. I keep receiving the following error message: ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at ...

The variable 'cache' is not recognized by Angular

Everything runs smoothly on my local Angular app, but once deployed on Heroku using a Go server, I encounter issues with aot being disabled in the Angular build on Chrome and Opera, particularly on mobile devices using Linux and OSX. However, Safari presen ...

After successful login, MSAL Angular will redirect the user to "/#code=..." either through a popup or using the msalGuard redirect method

When attempting to integrate MSAL 2.5.8 into my Angular 11 project and logging in with canActivate: [MsalGuard], the popup redirects to "http://localhost:4200/#code=0.AX...". I followed this github repository for implementing msal into my project. In my ...

What is the solution to the error message "Unable to assign property of undefined"?

I am currently working on an angular countdown timer and encountering a TypeError when attempting to access a variable from another component. I am struggling to identify the root cause of this issue. Here is the video tutorial that I am using as a referen ...

Navigable outside graphic key in AmCharts version 4

Is it possible to achieve a scrollable legend in AmCharts 4 similar to this AmCharts 3 tutorial? With the absence of legend.divId in AmCharts 4, controlling legend size seems challenging as it is all rendered as a single SVG element with limited control us ...