What is the best way to capitalize the initial letter in every word within a string array? Implementing a `for ... of ... loop` in ES6

const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];

for (const day of days) {
  console.log(day);
}

I am looking to display the days with the initial letters in uppercase...

Answer №1

transformedDays = days.map(day => day[0].toUpperCase() + day.substr(1))

Answer №2

Give this a shot:

function makeFirstLetterUppercase(text) {
    return text.charAt(0).toUpperCase() + text.slice(1);
}

Answer №3

To easily capitalize the first letter of each day, you can iterate over the days in a loop with this code snippet:

const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];

for (const day of days) {
  console.log(day[0].toUpperCase() + day.substr(1));
}

Answer №4

Hopefully this solution is beneficial

capitalizeFirstLetter(string);

Answer №5

Traditional approach:

const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];

var result = [];
for(var i = 0; i < days.length; i++){
result.push(days[i].charAt(0).toUpperCase() + days[i].substring(1));
}

console.log(result);

Answer №6

By utilizing the function map along with the regex pattern /(.?)/, you can easily replace the first captured letter with its uppercase version.

const months = ['january', 'february', 'march', 'april', 'may', 'june', 'july'];
var output = months.map(m => m.replace(/(.?)/, (letter) => letter.toUpperCase()));
console.log(output);

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

Encountering a 500 error in production when making a call to the Next.js API

I have a dedicated API folder within my next.js application to handle server-side endpoints: import { NextApiRequest, NextApiResponse } from 'next' import Cors from 'cors' // Setting up the CORS middleware const cors = Cors({ method ...

What is the process for creating a method within a Vue directive?

How can I define a local method inside my directive and utilize it within the bind and componentUpdated functions? Below is the code snippet in question: export const OutsideClick = { bind (el, binding, vnode) { console.log(new Vue()); // call ...

The navigation in Framework 7 is causing issues with the on-click functionality

Utilizing the framework's built-in formToJSON() function, I have been able to retrieve form values. By utilizing a click event, I am able to log the values. $$("#query-submit").on("click", function () { var queryForm = app.formToJSON("#query-form ...

React router-dom doesn't function properly when embedded in a nested organization

I am working on creating an authentication page with the following routes: /auth -> show auth status /auth/signin -> Sign in form /auth/signup -> Sign up form These are the components used in my App App.js function App() { return ( <Br ...

Modifying arrays in ReactJS

Having trouble editing my array list, need some help. I can update a single input value successfully, but struggling with updating the entire array. Any suggestions on why the method isn't working and how to edit the array? When I try to store data ...

Change the background of the play/stop icon with a jquery toggle

There is a list of video links with play icons as backgrounds in front of them. When a user clicks on a link, the video will start playing in a player located to the left of the links. The clicked link's background icon changes to a 'stop' i ...

Steps for showcasing smaller grids underneath a main grid

Hey there! I'm fairly new to the world of development and have recently started working with React. I've come across a scenario that has me stumped, so I thought I'd reach out for some assistance. In the image below (you can view it here), ...

Original: Placeholder Text With $scope.Array and HTML DIVRewritten: Text

How can I display default text as a placeholder in a drop-down menu without including it as an option? HTML <div class="form-group"> Upload new file to: <select class="form-control" ng-model="selectedDocumentType" ng-click="s ...

"Encountering an error searching for the chai module in the node modules folder even though

Attempting to install chai through the command line, I used the following command. npm install --save-dev chai After that, I attempted to run my unit test class with the specified imports: import {assert} from 'chai'; import {expect} from &ap ...

Issue encountered while utilizing property dependent on ViewChildren

Recently, I designed a custom component which houses a form under <address></address>. Meanwhile, there is a parent component that contains an array of these components: @ViewChildren(AddressComponent) addressComponents: QueryList<AddressCo ...

Transitioning to Vue 3: [Vue warning]: Prop already has a computed property named "actions"

Currently in the process of migrating a Vue 2 application to Vue 3, I've encountered an issue where I am frequently seeing this warning: [Vue warn]: Computed property "actions" is already defined in Props. This warning pops up in various c ...

Using the UI Bootstrap radio button within an ng-repeat loop

I'm attempting to create a series of radio buttons using ui bootstrap (http://angular-ui.github.io/bootstrap/) similar to the example on their website, but utilizing ng-repeat: <div class="btn-group"> <label ng-repeat='option in opt ...

Stop user from navigating to specific route using React-router

I am currently utilizing react-router with history useQueries(createHashHistory)(), and I have a requirement to restrict navigation to certain routes based on the route's configuration. The route configuration looks like this: <Route path="/" name ...

Proto-Type Namespace

I have a class named "Animals" which serves as a namespace for other classes, "Crocodile" and "Monkey": var Monkey = function(Animals) { this.Animals = Animals; }; Monkey.prototype.feedMe = function() { this.Animals.feed(); }; var Crocodile = functi ...

Incorporating an external JavaScript file into an AngularJS project

index.html: <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script> <script src="./assets/namesController.js"></script> <body ng-app="myApp"> < ...

What is the best way to implement a series of delayed animations in jQuery that are connected

Imagine you have the following items: <div id="d1"><span>This is div1</span></div> <div id="d2"><span>This is div2</span></div> <div id="d3"><span>This is div3</sp ...

The execution of 'observe' on 'MutationObserver' failed because parameter 1 is not the correct type of 'Node'. Ensure to use select2() instead

Attempting to implement select2 for dynamically loaded data via ajax, I am encountering the error mentioned above. What could be the issue? Below is the code snippet in question: $(document).on('change', '[name="country"]', fu ...

The error message "req.body undefined in NEXT JS" pops

Feeling lost and confused? I'm encountering an 'undefined' issue while attempting to upload my form data to Supabase. The data is being passed as undefined to the API, but when I inspect it within the submit handler, it displays correctly b ...

Is it possible to detect when a user reaches the end of a div without using a scroll event?

Seeking a JavaScript solution that can identify when a user reaches the bottom of a div with overflow: auto. While there are numerous solutions on Stack Overflow utilizing the onscroll event, I am curious if this can be accomplished using newer technology ...

Hovering over the top menu items in AngularJS will reveal dropdown submenus that will remain visible even when moving the cursor

I am facing an issue where my top menu has links that display a dropdown of additional menu items upon hovering. I have attempted to use onmouseover and onmouseleave events to control the visibility of the sub menu. However, I have encountered a problem ...