创建包含所有大写字母的饮料
var drinks = ["Coke", "Pepsi", "Water", "Orange Juice"];
console.log("First statement");
console.log(drinks[0]);
console.log(drinks[1]);
console.log(drinks[2]);
console.log(drinks[3]);
然后(分别)修改数组的每个元素以使其变为小写
答案 0 :(得分:1)
答案 1 :(得分:0)
如果需要更改原始数组,则可以使用函数forEach
来循环和修改每个索引值。
var drinks = ["Coke", "Pepsi", "Water", "Orange Juice"];
drinks.forEach((s, i, arr) => arr[i] = s.toLowerCase());
console.log(drinks);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:0)
var drinks = ["Coke", "Pepsi", "Water", "Orange Juice"].map(v => v.toLowerCase());
console.log(drinks);
或使用此代码,因为箭头功能在IE中根本无法使用:
var drinks = ["Coke", "Pepsi", "Water", "Orange Juice"].map(function(v) {
return v.toLowerCase();
});
console.log(drinks);
答案 3 :(得分:0)
如果要不可变地进行此操作,而又无需修改原始数组,请使用String.toLocaleLowerCase()
获取转换后的数组,并使用Array.forEach()
来小写您的项目:< / p>
const drinks = ["Coke", "Pepsi", "Water", "Orange Juice"];
const result = drinks.map(x => x.toLocaleLowerCase());
console.log(result);
否则,对于可变方法,请使用{{3}}:
const drinks = ["Coke", "Pepsi", "Water", "Orange Juice"];
drinks.forEach((x, i, arr) => arr[i] = x.toLocaleLowerCase());
console.log(drinks);