简体   繁体   English

如何在Javascript中将值从一维数组推到二维数组?

[英]How to push values from 1 dimensional array to 2 dimensional array in Javascript?

This is my first question on this forum. 这是我在这个论坛上的第一个问题。 I am a novice programmer. 我是新手程序员。 Currently, I working on an app which returns an English translated sentence of the passed binary string. 目前,我正在开发一个应用程序,该应用程序返回传递的二进制字符串的英语翻译句子。 Here is my code: 这是我的代码:

function binaryAgent(str) {      

  var binArr = str.split('');
  var res = [];
  var binary = [128, 64, 32, 16, 8 , 4, 2, 1];
  var k = -1;
  var matrix = [];
  var noSpace = str.replace(/\s+/g, '');

  for(var m=0; m<noSpace.length; m++){
      if(m % 8 === 0){
        k++;
        matrix[k] = []; 
      }
        matrix[k].push(noSpace[m]); 
  }

  for(var i=0; i<matrix.length; i++){
    for(var j=0; j<matrix[i].length; j++){
      if(matrix[i][j] == 1){
        res.push(binary[j]);
      }
    }
  }

  return res;
}

binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 
            01100010 01101111 01101110 01100110 01101001 01110010 01100101 
            01110011 00100000 01100110 01110101 01101110 00100001 00111111");

In the second for loop, I iterate through 'matrix' array to find values of 1. When I find it I am pushing appropriate value from 'binary' array. 在第二个for循环中,我遍历“矩阵”数组以找到值1。找到它时,我从“二进制”数组中推入了适当的值。 And I stuck in one place. 我被困在一个地方。

Function returns an array 'res' with values altogether: 函数返回一个数组“ res”,其值总计为:

[64, 1, 64, 32, 16, 2, 64, 32, 4, 1, 64, 32, 8, 4, 2, 32, 4, 2, 1, 64, 32, 16, 4, 32, 64, 32, 2, 64, 32, 8, 4, 2, 1, 64, 32, 8, 4, 2, 64, 32, 4, 2, 64, 32, 8, 1, 64, 32, 16, 2, 64, 32, 4, 1, 64, 32, 16, 2, 1, 32, 64, 32, 4, 2, 64, 32, 16, 4, 1, 64, 32, 8, 4, 2, 32, 1, 32, 16, 8, 4, 2, 1]

Problem is that I do not to know how to sum appropriate values in 'res' array. 问题是我不知道如何对'res'数组中的适当值求和。 I would get a return something like that: 我会得到这样的回报:

[[64, 1], [64, 32, 16, 2], [64, 32, 4, 1], [64, 32, 8, 4, 2] etc ..]

Then I will be able to sum values in particular arrays and after that using fromCharCode() I will return a sentence in English. 然后,我将能够对特定数组中的值求和,然后使用fromCharCode()返回英文句子。

Anyone know how to get an array like above? 任何人都知道如何获取上面的数组吗? Or to sum appropriate values in another way? 还是以其他方式求和适当的值?

If I understand your problem correctly, you should be able to use a similar technique in your second loop. 如果我正确理解了您的问题,则应该可以在第二个循环中使用类似的技术。 Just push a fresh array into res before starting your inner loop and then push values into that array in the inner loop: 只需在开始内部循环之前将一个新数组推送到res ,然后在内部循环中将值推送到该数组中即可:

 function binaryAgent(str) { var binArr = str.split(''); var res = []; var binary = [128, 64, 32, 16, 8 , 4, 2, 1]; var k = -1; var matrix = []; var noSpace = str.replace(/\\s+/g, ''); for(var m=0; m<noSpace.length; m++){ if(m % 8 === 0){ k++; matrix[k] = []; } matrix[k].push(noSpace[m]); } for(var i=0; i<matrix.length; i++){ res[i] = [] for(var j=0; j<matrix[i].length; j++){ if(matrix[i][j] == 1){ res[i].push(binary[j]); } } } return res; } var ar = binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"); console.log(ar) 

You didn't ask, but as an aside, you can also get rid of some of the loops if you want to make the code a little tidier: 您没有问,但顺便说一句,如果您想使代码更简洁,也可以摆脱一些循环:

 function binaryAgent(str) { var binary = [128, 64, 32, 16, 8 , 4, 2, 1]; var k = -1; var matrix = str.split(/\\s+/).map(i => Array.from(i)) return matrix.map(a => a.reduce((a, c, i) => { if(c == 1) { a.push(binary[i]) } return a }, [])) } var ar = binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"); console.log(ar) 

And here is a ready app: 这是一个现成的应用程序:

function binaryAgent(str) {

  var binArr = str.split('');
  var res = [];
  var binary = [128, 64, 32, 16, 8 , 4, 2, 1];
  var k = -1;
  var matrix = [];
  var noSpace = str.replace(/\s+/g, '');
  var sum = [], res2 = [], result = [];

  for(var m=0; m<noSpace.length; m++){
      if(m % 8 === 0){
        k++;
        matrix[k] = []; 
      }
        matrix[k].push(noSpace[m]); 
  }

  for(var i=0; i<matrix.length; i++){
    res[i] = [];
    for(var j=0; j<matrix[i].length; j++){
      if(matrix[i][j] == 1){
        res[i].push(binary[j]);
      }
    }
  }

  function getSum(total, num){
    return total + num;
  }

  for(var x=0; x<res.length; x++){
    for(var y=0; y<res[x].length; y++){
     sum[x] = res[x].reduce(getSum);
    }  
  }

  for(var z=0; z<sum.length; z++){
    res2[z] = String.fromCharCode(sum[z]);
  }

  result = res2.join('');

  return result;
}

binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");

I am aware that the code is not the highest quality, but today I have learned a lot. 我知道代码不是最高质量,但是今天我学到了很多东西。 Once again thanks to Mark_M! 再次感谢Mark_M!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM