Im doing an assignment that requires that given array be converted to new one so that new array consists of one first item, two second items, tree third items etc. without using loops, just array specific methods. For example:
[] => []
[ 1 ] => [ 1 ]
[ 'a', 'b' ] => [ 'a', 'b','b' ]
[ 'a', 'b', 'c', null ] => [ 'a', 'b','b', 'c','c','c', null,null,null,null ]
I have solved it by using .map and recursions. Function looks like this:
function propagateItemsByPositionIndex(arr) {
let newArray = [];
let additions = 0;
arr.map(function (k, x) {
createArray(k, x);
additions = 0;
});
return newArray
function createArray(item, count) {
if (additions <= count) {
newArray.push(item);
++additions
createArray(item, count);
}
}
}
It feels like there should be much better way to do this.