I am trying to read some code which is written in ruby. I am conversant with JavaScript and would like to understand what the code translates to in javascript.
Here is the code:
def two_d_translate(arr)
new_arr = []
arr.each do |subArray|
ele = subArray[0]
num = subArray[1]
num.times { new_arr << ele }
end
return new_arr
end
The aim of the code is to translate a two-dimensional array to a one-dimensional array by printing the string as many times as the number beside it which is the second element in each sub-array.
I used this to try to imitate it but I wonder if there is something else which could be better.
function two_d_translate(arr) {
let newArr = '';
let array = [];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j += 2) {
let ele = arr[i][j];
let num = arr[i][j+1];
if (num === 0){
continue;
}
array[i] = Array(num).fill(ele);
}
}
newArr = array.toString().split(',');
return newArr;
}
This seems to be cool for this test sample.
arr_1 = [
['boot', 3],
['camp', 2],
['program', 0]
]
console.log(two_d_translate(arr_1));
It will result in the expected output
[ 'boot', 'boot', 'boot', 'camp', 'camp' ]
but wouldn't in this case
arr_1=[
['boot', 3],
['camp', 0],
['program', 2]
]
this would result in an undesirable output which is
[ 'boot', 'boot', 'boot', '', 'program', 'program' ]
With Array#reduce
you are able to iterate over the entire array whilst pushing/concatenating the last calculation into the final result. This, in combination with Array.fill
, allows us to create the correct amount, sometimes 0, of the string. And with Array#concat
we can easily translate this 3D array into a 2D one. Here is an example:
function translateTo2D(arr) { return arr.reduce((result, [name, count]) => result.concat(Array(count).fill(name)), []); } const arr = [['a', 2], ['b', 0], ['c', 1]]; console.log(translateTo2D(arr));
If your environment allows spread syntax, you can do something like this
const arr_1 = [
['boot', 3],
['camp', 0],
['program', 2]
]
const result = arr_1.reduce((acc, [string, times]) => [...acc, ...Array(times).fill(string)], []);
console.log(result); // [ 'boot', 'boot', 'boot', 'program', 'program' ]
The trick is to use flatMap
to join separate runs together:
let translate = a => a.flatMap(([s, n]) => Array(n).fill(s)) console.log(translate([ ['boot', 3], ['camp', 2], ['program', 1] ]))
If your target doesn't support flatMap
yet, the equivalent ES6 idiom is [].concat(...map)
:
let translate = a => [].concat(...a.map(([s, n]) => Array(n).fill(s))) console.log(translate([ ['boot', 3], ['camp', 2], ['program', 1] ]))
As a rule of thumb, you use map/flatMap
when transforming "many things" to "many things", and reduce
for "many things" to "one thing".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.