[英]what is the equivalent of this code in javascript
I am trying to read some code which is written in ruby. 我正在尝试阅读一些用ruby编写的代码。 I am conversant with JavaScript and would like to understand what the code translates to in javascript.
我精通JavaScript,并且想了解代码在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. 使用
Array#reduce
您可以遍历整个数组,同时将最后的计算推入/连接到最终结果中。 This, in combination with Array.fill
, allows us to create the correct amount, sometimes 0, of the string. 结合
Array.fill
,我们可以创建正确数量的字符串,有时为0。 And with Array#concat
we can easily translate this 3D array into a 2D one. 借助
Array#concat
我们可以轻松地将此3D数组转换为2D数组。 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: 诀窍是使用
flatMap
将单独的运行连接在一起:
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)
: 如果您的目标尚不支持
flatMap
,则等效的ES6习惯用法是[].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". 根据经验,在将“许多事物”转换为“许多事物”时,可以使用
map/flatMap
,而将“许多事物” reduce
为“一件事物”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.