簡體   English   中英

將數組轉換為大寫

[英]Convert array into upper case

我有一個數組,我想將其更改為大寫,但我無法使其工作。 請幫我。

var array2 = ["melon","banana","apple","orange","lemon"];

array2.toUpperCase()

我也嘗試了以下方法,但它也不起作用。

array2.map(toUpperCase);

提前致謝!

你應該在地圖中使用處理函數:

var array2 = ["melon","banana","apple","orange","lemon"];
array2 = array2.map(function(x){ return x.toUpperCase(); })

有關地圖的更多信息

編輯:是的,你可以

toUpper = function(x){ 
  return x.toUpperCase();
};
array2 = array2.map(toUpper);

toUpperCase()不改變值。 嘗試

array2 = array2.map(function (e) { 
    return e.toUpperCase()
});

你可以使用這個:

var a = ['this','is','test'];

a.map(f=>{ return f.toUpperCase(); });

Map 是我推薦的方式,但這里有一個替代方法,可能有助於希望了解對象原型上使用的應用的人。

這是一個數組的示例,它通過 .apply() 借用 String 對象的 .toUpperCase() 方法並在所述數組上使用它。 使用 .apply() 將數組強制轉換為字符串,然后使用 .split() 方法將其轉換回大寫數組。

arr = ["fulu", "nulu", "hulu"];
var arrToUp = String.prototype.toUpperCase.apply(arr).split(",");

console.log(arrToUp)
//["FULU", "NULU", "HULU"]

嘗試循環遍歷每個元素並將其設為大寫。

for(var i = 0; i < array2.length; i++){
    array2[i] = array2[i].toUpperCase();
}

您可以將 toUpperCase() 方法添加到 Array.prototype:

Array.prototype.toUpperCase = function(){
   return this.map( (el) => (typeof el === "string") ? el.toUpperCase() : el)}

這可能是最簡單的解決方案,無需遍歷數組。

const yourArray = ["apple","banana"];
const upperCasedArray = String(yourArray).toUpperCase().split(",");
//OR
const upperCasedArray = yourArray.toString().toUpperCase().split(",");

發生的事情是首先我們將數組轉換為字符串,然后使用字符串的 toUpperCase() 方法將該字符串全部大寫,然后使用“,”將其拆分,因此我們得到一個大寫的新數組。

據我所知,JavaScript 並不真正支持數組的toUpperCase()函數。 您必須創建一個forforeach循環來循環遍歷數組並在數組中的每個元素上調用.toUpperCase()

您還可以像這樣創建一個小函數: StackOverflow 上的其他問題

var arr = ["hamed","hassan","hema","zeiad","saad","omar","ali","sayed","ahmed"];
var str = String(arr).toUpperCase().split(",");

下划線.js

如果您使用的是 Underscore.js 並且不能使用 ES6 細節,那么它是這樣的:

_.map( array2, function( value ){ return value.toUpperCase() } )

您可以輕松地將其轉換為uppercaselowercase

 var my_array = ["melon","banana","apple","orange","lemon"]; function convertToUppercase() { var e = document.getElementById('show'); e.innerHTML = "Uppercase: " + String(my_array).toUpperCase().split(","); console.log(String(my_array).toUpperCase().split(",")); } function convertToLowercase() { console.clear(); var e = document.getElementById('show'); e.innerHTML = "Lowercase: " + String(my_array).toLowerCase().split(","); console.log(String(my_array).toLowerCase().split(",")); }
 button { background: #0095ff; color: white; border: none; border-radius: 3px; padding: 8px; margin: 10px; cursor: pointer; -moz-box-shadow: 0 0 8px #999; -webkit-box-shadow: 0 0 8px #999; box-shadow: 0 0 8px #999; } p { margin: 0; }
 <button onclick="convertToUppercase()">Convert to Uppercase</button> <button onclick="convertToLowercase()">Convert to Lowercase</button> <p id="show"></p>

var array2 = ["melon","banana","apple","orange","lemon"];

array2.map(fruit => fruit.toUpperCase())

您正在嘗試更改數組的值。 為此,您必須先遍歷數組,然后再在數組上應用所需的方法。 這樣,每個值都將采用應用的方法。

const array2 = ["melon","banana","apple","orange","lemon"];

/*create another variable (newArr) which would store your "loop through array2"*/

const newArr = array2.map(arr => arr.toUpperCase());

console.log(newArr);

量子點

回顧一下:

有兩種主要方法可以做到這一點:

1. 使用帶有箭頭 function 和 map() 迭代器的現代 Javascript ES6 版本:

// the test array

const items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6'];

// declare the function with arrow function using the map iterator and the 
// toUpperCase() buildin method

const countItems = arr => arr.map(item => item.toUpperCase());

2. 使用老式的 function 風格,帶有一個簡單的 for 循環,toUpperCase() 內置方法和 push() 方法

// the test array

const items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6'];

// the old way

function countItems(arr) {
  newarray = [];
  for (let i = 0; i < arr.length; i++) {
    
    newarray.push(arr[i].toUpperCase());
  }
  return newarray;
};

3. 使用上述任何片段調用 function

console.log(countItems(items));

// 應該打印

['ITEM1','ITEM2','ITEM3','ITEM4','ITEM5','ITEM6']

希望這個答案可以為您想要構建的任何東西添加一塊小石頭

var name = ['Amit','Sumit','Kumit'];
var newname = name.join('-').toUpperCase().split();
console.log(newname);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM