Hi there,
How to solve this matter? It is in array.
I got 2 variables as below, and want it repeat depend on the var b;
var a = [5,10,15]
var b = [2,1,3]
the output should be like this;
[5,5,10,15,15,15]
Thanks in advance!
Hi there,
How to solve this matter? It is in array.
I got 2 variables as below, and want it repeat depend on the var b;
var a = [5,10,15]
var b = [2,1,3]
the output should be like this;
[5,5,10,15,15,15]
Thanks in advance!
This should do it.
var a = [5, 10, 15];
var b = [2, 1, 3];
function repeatValuesByAmounts(values, amounts) {
return values.reduce(function(arr, next, index) {
var i = amounts[index];
while (i > 0) {
arr.push(next);
i -= 1;
}
return arr;
}, []);
}
var c = repeatValuesByAmounts(a, b); // [5,5,10,15,15,15]
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.