繁体   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