簡體   English   中英

array.concat()的問題

[英]issues with array.concat()

我正在嘗試使用遞歸調用來連接返回數組

解決此問題的方法是:接收到數據流,並且需要將其反轉。

每個段的長度為8位,這意味着這些段的順序需要顛倒,例如:

11111111 00000000 00001111 10101010(byte1)(byte2)(byte3)(byte4)應該變成:

10101010 00001111 00000000 11111111(byte4)(byte3)(byte2)(byte1)總位數始終是8的倍數。

真的嘗試不同的組合...

 function dataReverse(data) { //split incoming array into array values consisting of 8 numbers each. //var octGroups = data.length / 8; var result = []; //recursive call function shuffler(array){ let input = array; //base case if(input.length === 0){ return result; } else { //concat result with 8 values at a time let cache = input.splice(-8,8); result.concat(cache); return shuffler(input); } return result; } var reversed = shuffler(data); //base case is if data.length === 0 return result else //reverse iterate through array, concating to new return array //return result return reversed; } console.log(dataReverse([1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0])); 

希望可以反向遍歷輸入數組,從末尾開始一次將一個結果數組包含8個值,但不能顛倒數字的順序。

我在上面的嘗試返回了一個零長度的數組。 我做錯了什么?

使用join代替concat

 function dataReverse(data) { //split incoming array into array values consisting of 8 numbers each. //var octGroups = data.length / 8; var result = []; //recursive call function shuffler(array) { let input = array; //base case if (input.length === 0) { return result; } else { //concat result with 8 values at a time let cache = input.splice(-8, 8); result.push(cache.join('')); return shuffler(input); } return result; } var reversed = shuffler(data); //base case is if data.length === 0 return result else //reverse iterate through array, concating to new return array //return result return reversed; } console.log(dataReverse([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0])); 

concat返回一個新數組,您需要將其分配回結果

result = result.concat(cache);

如果您希望每個字節為8個字符的字符串,則可以使用join

result = result.concat(cache.join(''));

 function dataReverse(data) { //split incoming array into array values consisting of 8 numbers each. //var octGroups = data.length / 8; var result = []; //recursive call function shuffler(array) { let input = array; //base case if (input.length === 0) { return result; } else { //concat result with 8 values at a time let cache = input.splice(-8, 8); result = result.concat(cache); return shuffler(input); } return result; } var reversed = shuffler(data); //base case is if data.length === 0 return result else //reverse iterate through array, concating to new return array //return result return reversed; } console.log(dataReverse([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0])); 

您可以循環遍歷數組,每組創建一個8字節的組,然后反轉,然后縮減為單個數組

 let dataReverse = (data) => { let count = 0 let temp = [] let group = data.reduce((op, inp) => { temp.push(inp) if (count === 8) { op.push(temp) temp = [] } return op }, []) if (temp.length) group.push(temp) return group.reverse().reduce((op,inp)=>op.concat(inp)) } console.log(dataReverse([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0])); 

concat返回一個數組-分配結果:

 function dataReverse(data) { //split incoming array into array values consisting of 8 numbers each. //var octGroups = data.length / 8; var result = []; //recursive call function shuffler(array) { let input = array; //base case if (input.length === 0) { return result; } else { //concat result with 8 values at a time let cache = input.splice(-8, 8); result = result.concat(cache); return shuffler(input); } return result; } var reversed = shuffler(data); //base case is if data.length === 0 return result else //reverse iterate through array, concating to new return array //return result return reversed; } let reversed = dataReverse([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0]); //Code for pretty-printing in groups of 8 reversed = reversed.reduce((acc, curr, i) => { const c = Math.floor(i / 8); acc[c] = [].concat((acc[c] || []), curr); return acc; }, []); console.log(reversed.map(e => e.join(""))); 
 .as-console-wrapper { max-height: 100% !important; top: auto; } 

此答案中的數組塊)。

您可以將數組8 pieces each ,然后僅使用Array.reverseArray.flat 這樣做的好處是您將獲得一個很好的功能,可重用,可鏈接且可讀性強的代碼。

考慮一下:

 let data = [1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0] // utility function to chunk array by number const chunkBy = (arr, by=2) => arr.reduce((r,c,i) => (i%by==0 ? r.push([c]) : r[r.length-1] = [...r[r.length-1], c], r), []) let result = chunkBy(data, 8).reverse().flat() console.log('in: ', data.join('')) console.log('out: ', result.join('')) 

這是更具可讀性的chunkBy函數:

 let data = [1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0] const chunkBy = (arr, by=2) => // default to chunks of 2 arr.reduce((acc, cur, index) => { if(index % by == 0) // use modulo to check for the remainder acc.push([cur]) // if exact then we start a new chunk else // if not we keep adding to the previous chunk acc[acc.length-1] = [...acc[acc.length-1], cur] return acc }, []) console.log(chunkBy(data, 8)) 

如果使用lodash _.chunk已經存在:

 let data = [1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0] let result = _(data) .chunk(8) .reverse() .flatten() .value() console.log(result.join('')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM