I have created an array of numbers with more than one digit and my idea is to map it so that the new array is the sum of the digits of each number.
Example: If I have the array1 [20, 33, 48] the array 2 would be [2, 6, 12] since I am adding from the first array firstly 2+0, secondly 3+3 and finally 4+8 .
This is what I have come to do but it is not giving me the result.
let numeros_array = [20, 33, 48, 34, 55, 30];
console.log(numeros_array);
let numeros_array2 = numeros_array.toString();
numeros_array2.from;
numeros_array2 = numeros_array.map(x => x+x);
console.log(numeros_array2);
Try this:
where:
where with:
toString()
: We convert to string the element of the mapped array in each cycle. Documentationmatch(/.{1}/g)
: We separate the string to convert it into an array of 1 character elements. Documentationreduce((SumaParcial, a)
: We reduce that array by performing the following operation on each element: ( Documentation )SumaParcial + parseFloat(a)
: returns the value of the accumulatorSumaParcial
with the numeric value of the passed element. Documentation, 0);
: is the initial value of the reduce.Several implementation examples have already been answered. In one way or another everyone does this
We have an array of numbers and we want to arrive at another array composed of their totals
We have to determine a function
numero_a_total
that for a numberx
whose digits areabc
calculates its sumnumero_a_total
can be represented as the combination of several functions:One that converts a number to its string representation
Another one that converts a string to an array of characters
Another one that converts characters to digits
And finally one that receives an array of digits and returns its sum
Which can be modeled as a pipeline
Implementations of
numero_a_string
. They are all equivalent:To convert that
string
tostring[]
(it's trivial to separate a string into its digits) you've also been given examples. The following are equivalentMap the result to an array of numbers . are equivalent
The crux: add array of numbers
You can use any loop
we can initialize x in the argument, we save a line
If it's to get original, I came up with this:
but
reduce
it is the most concise and elegant methodAnd if you want to reprocess an array adding the digits of its elements, and the result an array adding the digits of its elements, and so on until you have a number per element, you can do a recursion:
Bonus track
Thanks to this question I learned that it
Array.from
accepts a second parameter, a function (by default identity ) to map the elements of the array you are creating.The following consolidates the 'word_to_characters
y
characters_to_numbers` pipeline steps into one:So we could solve the problem with:
By steps:
I'm assuming this is a practice exercise, so I'll give you a guide instead of solving it at all:
You can use in (1) the method
String.split()
.You can use the method in (2)
Array.reduce()
to transform the list of digits into their sum. Be careful when adding or you will end up concatenating.here another option
It
.map
processes each number in the initial array.He
[...num.toString()]
converts the number into individual charactersHe
.reduce
converts each digit to a number and adds it