如何将字符串数组拆分为单个整数的二维数组

时间:2022-04-17 19:45:28

I am working on an exercise as a very beginner, self-taught (so far) programming student. I was given this array of credit card numbers:

我正在做一个非常初学者,自学成才(迄今为止)编程学生的练习。我得到了这一系列的信用卡号码:

var myArray = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-  7978', '4556-4242-9283-2260'];

I am to remove the dashes, get the sum of the individual integers of each credit card number, and return the highest sum.

我要删除破折号,获取每个信用卡号的各个整数的总和,并返回最高金额。

I have removed the dashes correctly:

我已正确删除破折号:

var myArray = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'];

var noDashes = myArray.map(function(x){ return x.replace(/-/g,"") });

console.log(noDashes);

Next I turned each string into an integer:

接下来,我将每个字符串转换为整数:

var strToInt = noDashes.map(function (x) { 
    return parseInt(x, 10); 

});

console.log(strToInt);

But now I cannot figure out how to iterate over the array and turn each long integer into it's single integers in a 2D array to look like:

但现在我无法弄清楚如何迭代数组并将每个长整数转换为2D数组中的单个整数,如下所示:

var newArray = [[4,9,1,6,2,6,0,0,1,8,0,4,0,5,3,0], [4,7,7,9,2,5,2,8,8,8,3,9,7,2], [4,2,5,2,2,7,8,8,9,3,7,9,7,8], [4,5,5,6,4,2,4,2,9,2,8,3,2,2,6,0]];

I know I need to use a for loop and possibly the .split method but I've been trying for two days with a million different searches and still have not come up with anything that works. I have found the complete answer to this exercise online, but it uses code way too advanced for my understanding, so I've been trying to break up the problem into blocks that I can understand.

我知道我需要使用for循环和可能的.split方法,但我已经尝试了两天,有一百万次不同的搜索,但仍然没有提出任何有用的东西。我已经在网上找到了这个练习的完整答案,但它使用的代码方式对我的理解来说太高级了,所以我一直试图将问题分解成我能理解的块。

I would appreciate some guidance! Thank you!

我很感激一些指导!谢谢!

2 个解决方案

#1


0  

To get the sum use reduce:

要获得总和使用减少:

oneArray.reduce(function(a,b) { return a+b })

To apply that to each one, use map:

要将其应用于每个,请使用map:

newArray.map(function(x) { return x.reduce(function(a,b) { return a+b }) })

To get the maximum sum, use Math.max:

要获得最大总和,请使用Math.max:

Math.max.apply(null, newArray.map(function(x) { return x.reduce(function(a,b) { return a+b }) }))

An extra exercise for you: which card number was it?

为您额外的锻炼:它的卡号是多少?

#2


0  

var myArray = [
  '4916-2600-1804-0530',
  '4779-252888-3972',
  '4252-278893-7978',
  '4556-4242-9283-2260'
];

var sums = myArray.map(function (ccValue) {
  return ccValue.replace(/[^0-9]/g, '').split('');
})
.reduce(function (a, b) {
  return a + b;
});

var largest = Math.max.apply(Math, sums);

console.log(largest);

#1


0  

To get the sum use reduce:

要获得总和使用减少:

oneArray.reduce(function(a,b) { return a+b })

To apply that to each one, use map:

要将其应用于每个,请使用map:

newArray.map(function(x) { return x.reduce(function(a,b) { return a+b }) })

To get the maximum sum, use Math.max:

要获得最大总和,请使用Math.max:

Math.max.apply(null, newArray.map(function(x) { return x.reduce(function(a,b) { return a+b }) }))

An extra exercise for you: which card number was it?

为您额外的锻炼:它的卡号是多少?

#2


0  

var myArray = [
  '4916-2600-1804-0530',
  '4779-252888-3972',
  '4252-278893-7978',
  '4556-4242-9283-2260'
];

var sums = myArray.map(function (ccValue) {
  return ccValue.replace(/[^0-9]/g, '').split('');
})
.reduce(function (a, b) {
  return a + b;
});

var largest = Math.max.apply(Math, sums);

console.log(largest);