修改Javascript数组元素 [英] Modify Javascript Array Element
本文介绍了修改Javascript数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有以下格式的数组元素(每对字母之间有一个空格字符,请忽略不属于实际元素的引号):
If I have an array element of the following format (with one whitespace character between each pair of letters, ignore the quote marks which are not part of the actual element):
arr[1] = "AB CD EF GH"
如何将其编辑为以下格式(每对字母都用*包围,并且每对字母之间有两个空格):
How can I edit this to be of the following format (with each pair of letters surrounded by * and two spaces between each pair of letters):
arr[1] = "*AB* *CD* *EF* *GH*"
推荐答案
使用分割
,地图
,然后使用 join
:
arr[1] = arr[1].split(' ').map(function (el) {
return '*' + el + '*';
}).join(' ');
这篇关于修改Javascript数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文