javascript - 数组[2,4,8]怎么样操作 让他变成(2 | 4 | 8)
问题描述
数组[2,4,8]怎么样操作 让他变成(2 | 4 | 8)??
[2,4,8].join('|') ==“2 | 4 | 8”,达不到想到的(2 | 4 | 8)这个形式. 反复用join 试了....外面的括号加不上去?请大家帮帮忙...
问题解答
回答1:join 2次
> [’(’,’)’].join([2, 4, 8].join('|'))’(2|4|8)’
@vue_小白
现在要把一个数组[1,2,4] 转换成数字类型的(1|2|4),因为后面需要把转化好的(1|2|4)转化成16进制..所以现在不知道如何把数组[1,2,4]转化成(1|2|4).toString(16)这样进行运算
方法如下:
> arr = [1,2,4]> arr.reduce((x,y)=>x|y,0).toString(16)’7’回答2:
'(' + [2, 4, 8].join('|') + ')'回答3:
1.哇,自己定制一个 prototype,默认的达不到。
var arr = [1, 2, 34];Array.prototype.FuckJoin = function (start, sp, end) { console.log(this); var res = start; this.forEach(function (a, index) { res += a.toString(); if (arr.length - 1 > index) { res += sp.toString(); } }) return (res + end);};console.log(arr.FuckJoin(’(’, ’|’, ’)’));
![javascript - 数组[2,4,8]怎么样操作 让他变成(2 | 4 | 8)](http://www.haobala.com/attached/image/news/202302/105338a789.png)
2.字符串两遍加一个 ()不就可以 了
相关文章:
1. java - 输出4个不相等整数之间所有任意三个整数的乘积2. angular.js - angular4 发出post请求 服务端显示OPTIONS3. python - 关于flask 静态文件配置404的问题4. python - urllib2.HTTPError: HTTP Error 400: Bad Request 求帮忙5. python 3.4 error: Microsoft Visual C++ 10.0 is required6. python - PyQt4怎么判断当前选中QComboBox列表中的项7. thinkphp5.0.24+PHPExcel导出功能总是导出不了,请大佬们帮忙分析原因8. python - beautifulSoup4 .select(’.bnrName’) 取不到到 text 文字9. vim - docker中新的ubuntu12.04镜像,运行vi提示,找不到命名.10. python3.x - 如何将python3.4的程序转为python2.7

网公网安备