Convert array into upper case
you should use handler function in map:
var array2 = ["melon","banana","apple","orange","lemon"];
array2 = array2.map(function(x){ return x.toUpperCase(); })
for more information about map
edit: yes you can do
toUpper = function(x){
return x.toUpperCase();
};
array2 = array2.map(toUpper);
How can I uppercase each element of an array?
Return a New Array
If you want to return an uppercased array, use #map:
array = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
# Return the uppercased version.
array.map(&:upcase)
=> ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"]
# Return the original, unmodified array.
array
=> ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
As you can see, the original array is not modified, but you can use the uppercased return value from #map anywhere you can use an expression.
Update Array in Place
If you want to uppercase the array in-place, use #map! instead:
array = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
array.map!(&:upcase)
array
=> ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"]
Capitalize first letter of each array element using JavaScript
You don't need to use .replace()
. Use .charAt(0)
to selecting first character of string and use .slice()
to selecting next part of string
var beg2 = "first,second,third";var ans22 = "";var words = beg2.split(",");
for (var i=0; i<words.length; i++){ words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1); ans22 += words[i] + "<br>"; }document.getElementById("ans22").innerHTML = ans22;
<div id="ans22"></div>
upperCase even characters and lowerCase odd characters in each element of an array in JavaScript
toUpperCase
doesn't modify the string in place (JavaScript strings are immutable). You need to assign the result back to a variable.
function toWeirdCase(string) { var reg = /\b(?![\s.])/ var res = string.split(reg) var newArr = []
for (let k = 0; k < res.length; k++) { let newString = ""; for (let j = 0; j < res[k].length; j++) { if (j % 2 == 0) { newString += res[k].charAt(j).toUpperCase() } else { newString += res[k].charAt(j).toLowerCase() } } newArr.push(newString) } return newArr.join('')}
console.log(toWeirdCase('This is a test'))
Formatting array elements into Uppercase and lower case format using JavaScript
You could use .map()
with .replace()
and the replacement method to convert your capital groups into lowercase groups like so:
const array1 = ["WORLDWIDE_AGRICULTURAL - CA"," WORLDWIDE_INDUSTRIAL - MX"];
const res = array1.map(str => str.replace(/(\w)(\w+)_(\w)(\w+)/g, (_, a, b, c, d) => `${a}${b.toLowerCase()} ${c}${d.toLowerCase()}` ).trim());
console.log(res);
How can I capitalize all the strings inside an array directly?
update: Xcode 8.2.1 • Swift 3.0.2
var dogNames = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"]
for (index, element) in dogNames.enumerated() {
dogNames[index] = element.capitalized
}
print(dogNames) // "["Sean", "Fido", "Sarah", "Parker", "Walt", "Abby", "Yang"]\n"
This is a typical case for using map()
:
let dogNames1 = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"].map{$0.capitalized}
A filter()
sample:
let dogNamesStartingWithS = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"].filter{$0.hasPrefix("S")}
dogNamesStartingWithS // ["Sean", "Sarah"]
you can combine both:
let namesStartingWithS = ["sean", "fido", "sarah", "parker", "walt", "abby", "yang"].map{$0.capitalized}.filter{$0.hasPrefix("S")}
namesStartingWithS // ["Sean", "Sarah"]
You can also use the method sort (or sorted if you don't want to mutate the original array) to sort the result alphabetically if needed:
let sortedNames = ["sean", "fido", "sarah", "parker", "walt", "abby", "yang"].map{$0.capitalized}.sorted()
sortedNames // ["Abby", "Fido", "Parker", "Sarah", "Sean", "Walt", "Yang"]
Trying to capitalize the first character in array of strings, why this is not working?
You have to actually re-assign the array element:
for(var i = 1 ; i < newArr.length ; i++){
newArr[i] = newArr[i].charAt(0).toUpperCase();
}
The "toUpperCase()" function returns the new string but does not modify the original.
You might want to check to make sure that newArr[i]
is the empty string first, in case you get an input string with two consecutive dashes.
edit — noted SO contributor @lonesomeday correctly points out that you also need to glue the rest of each string back on:
newArr[i] = newArr[i].charAt(0).toUpperCase() + newArr[i].substr(1);
Related Topics
How Would I Go About Programmatically Interacting with VSt(I) Plugins to Synthesize Audio
What Do the Fields of Ruby's Gc.Stat Mean
Ruby Create Recursive Directory Tree
Singleton Method VS. Class Method
How to Use Rspec Expectations in Irb
How to Look Up Elevation Data by Lat/Lng
Resetting a Singleton Instance in Ruby
How to Split a String into Only Two Parts, by the Last Occurrence of the Split Char
Circular Dependency Detected While Autoloading Constant When Loading Constant
How to Specify "Http Request Header" in Openuri
Rails: Switch Connection on Each Request But Keep a Connection Pool
What Are the Advantages of Mocha Over Rspec's Built in Mocking Framework
What's the Difference Between Arrays and Hashes
Activerecord 3.1.0 Multiple Databases