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(’(’, ’|’, ’)’));
2.字符串兩遍加一個 ()不就可以 了
相關文章:
1. java - Spring boot 讀取 放在 jar 包外的,log4j 配置文件,系統有創建日志文件,不寫入日志信息。2. sass - gem install compass 使用淘寶 Ruby 安裝失敗,出現 4043. node.js - express 4.x 如何在不同js文件中處理router請求4. python 3.4 error: Microsoft Visual C++ 10.0 is required5. 爬蟲圖片 - 請教各位:python爬蟲編碼問題,版本3.6,win10 64位下?6. javascript - video標簽播放mp4視頻自動暫停?7. javascript - angular2/4中,組織一個頁面的容器應該是component還是module?8. javascript - log4js的使用問題9. 網頁爬蟲 - python3.4.1 request模塊報錯 ’list’ object has no attribute ’get’10. mysql - 連續的24條sql語句,一條條查,還是union all好?
