Deleting the last item from an array in Typescript

Consider the following array :

["one-", "two-", "three-", "testing-"]

Once converted into a string, it looks like this:

"one-,two-,three-,testing-"

I need to remove the last character (hyphen) after 'testing' and create a new array from it.

The desired output is:

["one-", "two-", "three-", "testing"]

Any assistance would be greatly appreciated.

Answer №1

If you're looking for a refined solution, consider utilizing .slice(0, -1) on the given string:

let updatedString = "one-,two-,three-,testing-".slice(0, -1); // "one-,two-,three-,testing"

To convert the updated string into a new array, simply apply .split(',')‌:

let newArr = updatedString.split(','); // ["one-", "two-", "three-", "testing"]

Answer №2

Follow these steps:

let sentence = "apple-,banana-,cherry-,coding-";
sentence = sentence.substring(0, sentence.length - 1);

Remove the last character from the string using the above code snippet. Next, transform it into a list:

let newList = sentence.split(",");

Answer №3

let items = ["apple-", "banana-", "cherry-", "testing-"];
let dataString = items.toString();
let resultString = dataString.replace("-", "");
var output = resultString.split(",");
console.log(output);

give this a go

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

Ways to trigger the keyup function on a textbox for each dynamically generated form in Angular8

When dynamically generating a form, I bind the calculateBCT function to a textbox like this: <input matInput type="text" (keyup)="calculateBCT($event)" formControlName="avgBCT">, and display the result in another textbox ...

What is the process for sending an HTTP post request with a React/Typescript frontend and a C#/.Net backend?

In the frontend code, there is a user login form that accepts the strings email and password. Using MobX State Management in the userstore, there is an action triggered when the user clicks the login button to submit the strings via HTTP post. @action logi ...

Wrapping text around an image using two distinct Angular components

Can text wrap around an image in Angular even if they are in separate components? Or do the text and image have to be within the same component for this to work, regardless of whether the image is on the left or right side? https://i.stack.imgur.com/hdlxD ...

Using AngularJS to iterate through a nested array with ng-repeat

Hey everyone, I've been working on a project where I have an array called menu[] with nested arrays called flavors[] within it. My goal is to calculate the total price of all active items and flavors in the menu. While I was able to successfully calcu ...

The arrow function in Jest is missing a name property

Currently, my setup includes: node.js: 9.8.0 Jest: 23.4.2 ts-jest: 23.1.3 typescript: 2.9.2 While attempting the following in my *.test.ts files: const foo = () => 'bar'; console.log(foo.name); // '' foo contains the name pro ...

Passing an array from JavaScript to PHP and then storing it as a .json file

Query I am trying to pass an array to PHP and save it in a data.json file. However, the data.json file is being created but showing Null as output. I have spent about 2 hours on this code, checked numerous solutions on SO, but nothing seems to work. As I ...

Reading Useless Information from an Input File

In my C++ program, I am working with reading a file in chunks that contain integers (two per line). To begin, I am using the following code snippet to determine the length of the file: input.seekg(0, input.end); int length = input.tellg(); input.seekg(0, ...

Display the first item last in an *ngFor loop in Nativescript Angular

I'm facing some confusion with the sorting of an array in JavaScript. Although the index, last, and first seem to be correct, the result is not as expected. Versions @angular/core: 4.1.0 nativescript-angular: 3.1.3 typescript: 2.4.0 Expected 1234 ...

Issue with Angular 10: Modal window fails to open upon second click

Encountering an issue with a Modal window overlapping the navigation bar and overlay due to a third-party bundle.js adding dynamic classes and divs under the parent-container. The problem arises from a fixed positioning of one of the classes which causes t ...

"Combining the power of AngularJS2 beta with Spring Boot: the ultimate

I am currently using Atom 1.4.0 with the atom-typescript package to develop AngularJS2 modules in TypeScript. On the backend, I have a spring-boot application for handling the REST APIs. After making changes to the .ts files in Atom, it seems to compile t ...

Having trouble retrieving the "Text" from the "element" with the help of "Protractor"

Greetings! I am currently in the process of creating Protractor automation scripts for an "Angular 4" application. Below is the code from my development: <!--Byndd Title div--> <div class="ui-g-12 ui-md-8 ui-lg-10"> ...

Enhancing the efficiency of Angular applications

My angular application is currently coded in a single app.module.ts file, containing all the components. However, I am facing issues with slow loading times. Is there a way to improve the load time of the application while keeping all the components within ...

Angular 6 canvas resizing causing inaccurate data to be retrieved by click listener

The canvas on my webpage contains clickable elements that were added using a for loop. I implemented a resizing event that redraws the canvas after the user window has been resized. Everything works perfectly fine when the window is loaded for the first ti ...

What is the best way to show summary calculations in the footer of a PrimeNG table?

I'm struggling to figure out how to create a summary function at the end of a PrimeNG p-table. I need to be able to calculate totals or minimum values for specific columns in the table based on the visible rows. My initial attempt involved trying to ...

Adjusting the position of Angular Mat-Badge in Chrome browser

I'm currently using the newest version of Angular and I am attempting to utilize the Angular materials mat-badge feature to show the number of touchdowns a player has thrown. However, in Chrome, the badge position seems to shift when the number is inc ...

Troubles with implementing child routes in Angular 6

I'm having trouble getting the routing and child routing to work in my simple navigation for an angular 6 app. I've configured everything correctly, but it just doesn't seem to be working. Here is the structure of my app: └───src ...

Displaying JSON Object in Kendo UI Grid with Incorrect Format

I encountered an issue where a function that I passed to a Kendo Grid field in the fetch method returns perfectly on console.log, but only [object Object] is returned in the Kendo Grid display. Here's the background: I am utilizing two services - Rev ...

What is the best way to activate a submit button when at least one checkbox is selected?

I checked out Mark's solution on Stack Overflow regarding disabling a button if no checkboxes are selected in Angular2. However, I have more than one array to consider. How can I adjust the code provided in the link for my situation? Alternatively, d ...

Launch a component in a separate browser tab

I decided to open my component "template1" in a new tab, so I set up the path as follows: const routes: Routes = [ {path : 'template1' , component: Template1Component} ]; In the HTML file: <a mat-raised-button color="primary" target="_bl ...

When using ng g module my-module command, it will only create the module.ts file without

When running the command ng g module my-module, only the module.ts file is generated. However, I require all the additional files such as css, HTML, ts, spec, and module.ts. Is there a way to generate all of these at once using the CLI? ...