array.concat()的问题

时间:2019-06-01 03:31:10

标签: javascript arrays ecmascript-6

我正在尝试使用递归调用来连接返回数组

针对此问题的方向是: 数据流已接收,需要反转。

每个段的长度为8位,这意味着这些段的顺序需要颠倒,例如:

11111111 00000000 00001111 10101010  (字节1)(字节2)(字节3)(字节4) 应该变成:

10101010 00001111 00000000 11111111  (字节4)(字节3)(字节2)(字节1) 总位数始终是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个值,但不会逆转数字的顺序。

我在上面的尝试返回了一个零长度的数组。我做错了什么?

4 个答案:

答案 0 :(得分:2)

使用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]));

答案 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;
}

this answer中的数组块)。

答案 2 :(得分: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]));

答案 3 :(得分:0)

您可以通过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>