```js
function capAllEl(arr) {
if (Array.isArray(arr)) {
arr.forEach((el, index, array) => {
if (typeof el === 'string') {
console.log( array[index] = el.toUpperCase());
}
});
} else {
console.error('arr is not an array');
}
};
capAllEl(['harry','potter','xiaorou']);
//结果:
//HARRY
//POTTER
//XIAOROU
```
更简洁的方法
```js
function capitalizeElements(arr) {
return arr.map(function(element) {
return element.toUpperCase();
});
}
// 测试这个函数
console.log(capitalizeElements(['hello', 'world', 'openai']));
```
- `.map` 将调用每个给出的数组字符
- `element`代表数组元素,`.toUpperCase`大写每个字符元素