在JavaScript中从单维数组返回二维数组

时间:2021-10-29 12:47:39

I have a single dimension array of a series of numbers:

我有一系列数字的单维数组:

var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];

I'm trying to insert the sum of the digits of each number alongside it to create a two-dimensional array, so that the output is:

我试图在它旁边插入每个数字的数字之和来创建一个二维数组,以便输出为:

[ [ '9493-4937-288383-8473', 96 ],
[ '4838-38403-8484', 65 ],
[ '9384-3848-4978-4944', 96 ],
[ '3920-2108-2845-1904', 58 ] ]

Unfortunately, my code:

不幸的是,我的代码:

for (var i = 0; i < x.length; i ++) {
  var y = x[i].replace(/[- )(]/g,'');

  var sum = 0;
  var z = y;
  while (z > 0) {
    sum = sum + z % 10;
    z = Math.floor(z / 10);
  }

  var xWithSum = [x[i]]; 

  xWithSum.push(sum);

  console.log(xWithSum);

}

results in the following output instead:

而是导致以下输出:

[ '9493-4937-288383-8473', 96 ]
[ '4838-38403-8484', 65 ]
[ '9384-3848-4978-4944', 96 ]
[ '3920-2108-2845-1904', 58 ]

That is, I'm ending up with four separate two-dimensional arrays rather than one two-dimensional array with four items.

也就是说,我最终得到了四个独立的二维数组,而不是一个带有四个项目的二维数组。

Will someone please show me the error of my (newbie) JavaScript ways?

有人请告诉我我的(新手)JavaScript方式的错误吗?

7 个解决方案

#1


3  

You need to push xWithSum onto a result array.

您需要将xWithSum推送到结果数组。

var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];
var result = [];
for (var i = 0; i < x.length; i++) {
  var y = x[i].replace(/[- )(]/g, '');

  var sum = 0;
  var z = y;
  while (z > 0) {
    sum = sum + z % 10;
    z = Math.floor(z / 10);
  }

  var xWithSum = [x[i], sum];
  result.push(xWithSum);
}

console.log(result);

You could also use .map() to run a function on each element of an array and return an array of the results.

您还可以使用.map()在数组的每个元素上运行函数并返回结果数组。

var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];
var result = x.map(function(el) {
  var y = el.replace(/[- )(]/g, '');

  var sum = 0;
  var z = y;
  while (z > 0) {
    sum = sum + z % 10;
    z = Math.floor(z / 10);
  }

  var xWithSum = [el, sum];
  return xWithSum;
});

console.log(result);

#2


3  

You could just iterate over the elements and over the characters for summing.

你可以迭代元素和字符进行求和。

var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'],
    y = x.map(function(a) {
        return [a, a.split('').reduce(function(a, b) {
            return a + (Number(b) || 0);
        }, 0)];
    });

console.log(y);

#3


1  

One more "you could just":

还有一个“你可以”:

x.map(function (num) {
  return [num, eval(num.replace(/\D*(\d)\D*/g, "$1+") + "0")];
});

#4


0  

Instead of

代替

for (...) { 
    ...
    console.log(x);
}

do something like

做点什么

var result = [];
for (...) {
    ...
    result.push(x);
}
console.log(result);

Also your way of counting the sum will not work, as you are trying to load a 16 digit integer into a javascript number. That will not work, javascript numbers doesn't have that much precision. You can calculate the sum instead this way:

此外,您计算总和的方式也不起作用,因为您尝试将16位整数加载到javascript编号中。这不起作用,javascript数字没那么精确。您可以这样计算总和:

var sum = 0;
str.replace(/\d/g, x => sum+= +x);
console.log(sum);

#5


0  

Couldn't you just... create an array above it and push the result to that array?

难道你不能......在它上面创建一个数组并将结果推送到该数组?

function converArr (x) {
  resultArr = [];

  for (var i = 0; i < x.length; i ++) {
    var y = x[i].replace(/[- )(]/g,'');

    var sum = 0;
    var z = y;
    while (z > 0) {
      sum = sum + z % 10;
      z = Math.floor(z / 10);
    }

    var xWithSum = [x[i]]; 

    xWithSum.push(sum);
    resultArr.push(xWithSum);
  }

  return resultArr;
}

convertArr(['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904']);

#6


0  

var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];

// Loop through everything
for (var i = x.length - 1; i >= 0; i--) {

    // Replace each element with [element, digitsSum]
    x[i] = [x[i], getDigitsSum(x[i])];

}

// x now is as described

#7


0  

You can use Array.prototype.reduce to reduce your array of strings into the required format as below:

您可以使用Array.prototype.reduce将字符串数组缩减为所需格式,如下所示:

var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];

var result = x.reduce(function(prev, current) {
    var curr = current.replace(/-/g, "");
    var sum = curr.split("").reduce(function (b, a) {
      return parseInt(a, 10) + b;
    }, 0);

    prev.push([current, sum]);  
    return prev;
}, []);

console.log(result);

#1


3  

You need to push xWithSum onto a result array.

您需要将xWithSum推送到结果数组。

var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];
var result = [];
for (var i = 0; i < x.length; i++) {
  var y = x[i].replace(/[- )(]/g, '');

  var sum = 0;
  var z = y;
  while (z > 0) {
    sum = sum + z % 10;
    z = Math.floor(z / 10);
  }

  var xWithSum = [x[i], sum];
  result.push(xWithSum);
}

console.log(result);

You could also use .map() to run a function on each element of an array and return an array of the results.

您还可以使用.map()在数组的每个元素上运行函数并返回结果数组。

var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];
var result = x.map(function(el) {
  var y = el.replace(/[- )(]/g, '');

  var sum = 0;
  var z = y;
  while (z > 0) {
    sum = sum + z % 10;
    z = Math.floor(z / 10);
  }

  var xWithSum = [el, sum];
  return xWithSum;
});

console.log(result);

#2


3  

You could just iterate over the elements and over the characters for summing.

你可以迭代元素和字符进行求和。

var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'],
    y = x.map(function(a) {
        return [a, a.split('').reduce(function(a, b) {
            return a + (Number(b) || 0);
        }, 0)];
    });

console.log(y);

#3


1  

One more "you could just":

还有一个“你可以”:

x.map(function (num) {
  return [num, eval(num.replace(/\D*(\d)\D*/g, "$1+") + "0")];
});

#4


0  

Instead of

代替

for (...) { 
    ...
    console.log(x);
}

do something like

做点什么

var result = [];
for (...) {
    ...
    result.push(x);
}
console.log(result);

Also your way of counting the sum will not work, as you are trying to load a 16 digit integer into a javascript number. That will not work, javascript numbers doesn't have that much precision. You can calculate the sum instead this way:

此外,您计算总和的方式也不起作用,因为您尝试将16位整数加载到javascript编号中。这不起作用,javascript数字没那么精确。您可以这样计算总和:

var sum = 0;
str.replace(/\d/g, x => sum+= +x);
console.log(sum);

#5


0  

Couldn't you just... create an array above it and push the result to that array?

难道你不能......在它上面创建一个数组并将结果推送到该数组?

function converArr (x) {
  resultArr = [];

  for (var i = 0; i < x.length; i ++) {
    var y = x[i].replace(/[- )(]/g,'');

    var sum = 0;
    var z = y;
    while (z > 0) {
      sum = sum + z % 10;
      z = Math.floor(z / 10);
    }

    var xWithSum = [x[i]]; 

    xWithSum.push(sum);
    resultArr.push(xWithSum);
  }

  return resultArr;
}

convertArr(['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904']);

#6


0  

var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];

// Loop through everything
for (var i = x.length - 1; i >= 0; i--) {

    // Replace each element with [element, digitsSum]
    x[i] = [x[i], getDigitsSum(x[i])];

}

// x now is as described

#7


0  

You can use Array.prototype.reduce to reduce your array of strings into the required format as below:

您可以使用Array.prototype.reduce将字符串数组缩减为所需格式,如下所示:

var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];

var result = x.reduce(function(prev, current) {
    var curr = current.replace(/-/g, "");
    var sum = curr.split("").reduce(function (b, a) {
      return parseInt(a, 10) + b;
    }, 0);

    prev.push([current, sum]);  
    return prev;
}, []);

console.log(result);