JavaScript:提取数组中每个字符串的第一个字母? [英] JavaScript: Extract first letter of every string in array?

查看:301
本文介绍了JavaScript:提取数组中每个字符串的第一个字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编码方面还很陌生,所以对大家来说可能是一个简单的问题。
如果我有此数组并只想显示它的每个元素的第一个字母,该怎么办?

I'm quite new in coding, so might be an easy question for you all. What exactly should I do if I have this array and want to just show the first letter of every element of it?

var friends = ["John", "Will", "Mike"];

我想用 substr 方法做到这一点但是我只想知道如何使用字符串。

I wanted to do it with substr method but I just want to know how to do it with a string.

推荐答案

使用 Array.map() 迭代数组,使用 ,然后返回它:

const friends = ["John", "Will", "Mike"];
const result = friends.map(([v])=> v);
console.log(result);

这篇关于JavaScript:提取数组中每个字符串的第一个字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆