Skip to content Skip to sidebar Skip to footer

Complete Missing Sequence In Array With Zeros In Javascript

I have the following array: I need to ensure that the first element of the array is a sequence from 5 to 10: [5, 6, 7, 8, 9, 10] In the above example, these numbers within the seq

Solution 1:

You could map the missing parts with a closure over the actual index of the given array.

const
    array = [[5, 0.2], [7, 0.6], [8, 0.3], [10, 0.4]],
    result = Array.from(
        { length: 6 },
        (i =>(_, j) => array[i]?.[0] === j + 5 ? array[i++] : [j + 5, 0])(0)
    );

console.log(result);

Post a Comment for "Complete Missing Sequence In Array With Zeros In Javascript"