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

Issue with esbuild not executing within docker compose configuration

Currently, I am new to using esbuild and struggling to set up a script that can watch and rebuild my files. Additionally, I need this functionality to work within a docker environment. Within my package.json file, the following scripts are defined: " ...

I encountered an issue with Angular where it is throwing an error stating that the property 'catch' does not exist on the type 'Observable<Object>'

I have been working on an angular application that interacts with a python flask API. During development, I encountered the need to display results passed from the backend. To achieve this, I created an angular service. Below is the code for the angular s ...

Leveraging a function for filtering in AngularJS

I have developed an application where the content filter changes depending on which button is clicked. I have managed to figure out the straightforward filters, but now I am attempting to create a filter that displays content within a specified range. Bel ...

Troubleshooting: Angular 2 router events subscription not triggering

Currently, I am utilizing a breadcrumbs component that subscribes to the router events. However, there is an issue where the subscription does not trigger the first time the component loads. ngOnInit(): void { console.log("oninit beradcumbs"); this.ro ...

What causes TypeScript 3.7.5 to trigger an error while typing a function that accepts an array as a parameter?

I'm facing a perplexing compiler error while trying to define a function that requires an array as its sole argument. Below is a concise scenario to reproduce the issue: http://www.example.com import React from 'react' type FooProps = { ...

Implementing individual NGRX Selectors for each child component to enable independent firing

My component serves as a widget on a dashboard, and I am using *ngFor to render multiple widgets based on the dashboard's data. Each WidgetComponent receives some of its data via @Input() from the parent. parent <app-widget *ngFor="let widget ...

When using RxJS forkjoin, it will cease subscription if there is a flatMap present within one of the observables it is awaiting

I have been experimenting with nested calls using rxjs and trying to implement nested forkJoins. However, I have encountered an issue where the outer forkJoin stops returning a result when there is a flatMap inside the inner observable. Here is a snippet ...

What is preventing me from using the "@" symbol to substitute the lengthy relative path in my __tests__ directory?

When I initialized my Vue project using vue-cli, I encountered an issue where the use of @ in my src folder was functioning correctly, but not in my __tests__ folder. I have already added configurations to my tsconfig.json file, although I am unsure if i ...

The ngIf directive did not initialize the observable in the child component as expected

I'm facing an issue with CompA and CompB in my Angular project. CompA has a child component called CompB, which has an ngIf condition based on an observable that is shared between the two components. When I set this condition to true and call next on ...

There are no route parameters defined

Within my user template file, I have a tab control implemented as shown below: <nav md-tab-nav-bar> <a class="tab-label" md-tab-link [routerLink]="'following'" routerLinkActive #following="routerLinkActive" [acti ...

Filtering an array dynamically in Typescript depending on the entered value

My task involves filtering arrays of objects based on input field values. Data data: [{ taskname: 'Test1', taskId: '1', status: 'Submitted' }, { taskname: 'Test2', taskId: '2', status: 'Re ...

Turning off @Output as Observable: A step-by-step guide

I have a query regarding unsubscribing Outputs in Angular. While I am aware that EventEmitter is automatically cleaned up, there was a time when I needed to use an Observable as my Output. Specifically, I wanted to take an Output that emitted events at mos ...

Issue: Unable to determine all parameters for FirebaseDataService

I've been working on abstracting and concealing the implementation of my DataService through the setup below: DataService: import { Observable } from 'rxjs/Observable'; export abstract class DataService { abstract getMyData(): Observa ...

Angular: Elf facade base class implementation for utilizing store mechanics

What is the most effective way to access the store within a facade base class, allowing for the encapsulation of commonly used methods for interacting with the store? Imagine we have a store (repository) export class SomeRepository { private readonly s ...

Angular 2 now has the ability to detect keydown events triggered from any part of the page

Is there a way to enable keyboard control for a view? I attempted using document.keydown, but it wasn't successful. How can I make the document accept keyboard input? A solution that worked for me was implementing this in my component class: @HostLi ...

In Angular, use the ngFor directive to iterate through items in a collection and add a character to each item except

Currently, I am iterating through my data list and displaying it in the view using spans: <span *ngFor="let d of myData"> {{d.name}}, </span> As shown above, I am adding a comma ',' at the end of each item to ensure a coherent displ ...

Issues arising while fetching data with Angular httpClient

Hey there! I'm currently working with a basic controller that is returning some simple data. Here's the code snippet: [HttpGet("[action]")] public AppSettings GetStuff() { var stuff = new Stuff { Dummy = "Hi" }; return stuf ...

Angular powered bootstrap modal with a clean and sleek design featuring a transparent header and background

Currently, I am utilizing an Angular-powered Bootstrap modal for a project. The modal content consists of an iframe that functions as a YouTube video player. My objective is to have the background colors of both the modal body and header be transparent, so ...

Ways to display an icon in Angular 10 only when it is hovered over

Just getting started with Angular and still learning the concepts. Trying to figure out how to show an icon only when it's hovered over. Can anyone assist me? Sorry, can't share the code because of company rules. The icons are for sorting and ...

Experiencing problems with npm installation while trying to compile Angular2 source code

I am trying to compile angular2 source code on my Windows 8.1 x64 development machine. The versions of Node and Npm I have are as follows: Node version 5.1.0 Npm version 3.3.12 1) Successfully cloned the repository 2) Executed bower install command - S ...