0

var foo = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

How would you turn the above into something like below?

var rev = [0, 1, 5, 4, 3, 2, 6, 7, 8, 9];

As you can see rev is only partially reversed. The reverse operation started at index 2 and ended at index 5

Just to make the end result clearer to see:

var foo = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

var rev = [0, 1, 5, 4, 3, 2, 6, 7, 8, 9];

NYC Tech Engineer
  • 1,677
  • 2
  • 20
  • 23

4 Answers4

0
var foo = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

Copying a portion of foo array starting at index 2 until just before index 6:

var bar = foo.slice(2,6);
bar.reverse();

Splice out the portion you'd like from foo, and insert bar in its place:

foo.splice(2, 4, bar)

Flatten array:

var rev = [].concat.apply([], foo);
Nisarg
  • 13,121
  • 5
  • 31
  • 48
NYC Tech Engineer
  • 1,677
  • 2
  • 20
  • 23
  • Credit goes to Nikita Volkov for showing me how to flatten an array in JavaScript: https://stackoverflow.com/a/15397506/2359049 – NYC Tech Engineer Apr 10 '18 at 05:55
  • Why did you ask a question and then answer yourself 5 mins later? – JohanP Apr 10 '18 at 05:57
  • @JohanP because it's encouraged by the inventor of this website – NYC Tech Engineer Apr 10 '18 at 05:59
  • @JaromandaX yeah, that's a good note, I didn't want to distract from the message, but people should be made aware. – NYC Tech Engineer Apr 10 '18 at 05:59
  • @NYCTechEngineer I'm confused, did you actually want this solved or did you already have a solution, you just wanted to post a question and then show people the solution? – JohanP Apr 10 '18 at 06:00
  • @JohanP yeah, why not? – NYC Tech Engineer Apr 10 '18 at 06:01
  • Are you kidding me? You think this is a place to showcase solutions that no one has asked for? – JohanP Apr 10 '18 at 06:03
  • @JohanP If the inventor of the website says it's okay to do, I'm going to assume it's okay to do. – NYC Tech Engineer Apr 10 '18 at 06:03
  • @JohanP ah, well, simply a matter of bad UX, the checkbox to answer your own question should have been greyed out for 48 hours. – NYC Tech Engineer Apr 10 '18 at 06:05
  • @JohanP Wait, I haven't accepted my own answer yet, and wasn't intending to until a few days have passed. So essentially, I have done nothing wrong at all if you read that link you shared with me. – NYC Tech Engineer Apr 10 '18 at 06:06
  • @JohanP my point about bad UX earlier still stands if the website allows users to accept their own answers before the 48 hour limit. – NYC Tech Engineer Apr 10 '18 at 06:07
  • @Nisarg sorry, I didn't realize you were talking to him, wasn't thinking straight for a moment. – NYC Tech Engineer Apr 10 '18 at 06:08
  • I knew you could self answer questions but I have always thought that the reason why that is done is because you have an actual question that you need answering then after finding a solution, you accept it. I have never thought you can just post a question even tho you already know the answer. – JohanP Apr 10 '18 at 06:10
  • Seems like it leaves the system open for abuse, unless you get no reputation for self accepting your answers – JohanP Apr 10 '18 at 06:12
  • @JohanP I think it's a win win: you can self-document your own answers and share it with the community at the same time. – NYC Tech Engineer Apr 10 '18 at 06:13
0

To avoid mutating the original array:

function reversePartArray(array, begin, length) {
    array = array.slice();
    array.splice(begin, length, ...array.slice(begin, begin+length).reverse());
    return array;
}
var foo = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var rev = reversePartArray(foo, 2, 4);
console.log(foo+'');
console.log(rev+'');

or

function reversePartArray(array, begin, length) {
    array = array.slice();
    array.splice(begin, 0, ...array.splice(begin,length).reverse());
    return array;
}
var foo = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var rev = reversePartArray(foo, 2, 4);
console.log(foo+'');
console.log(rev+'');
Jaromanda X
  • 47,382
  • 4
  • 58
  • 76
0

Here's a good old iterative solution:

let foo = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

let reverse = (array, start, end) => {
  while (start < end) {
    let t = array[start];
    array[start++] = array[end];
    array[end--] = t;
  }
};

reverse(foo, 2, 5);

console.log(foo.toString());
Kirill Simonov
  • 7,206
  • 3
  • 15
  • 36
0

You could just map the values with a check if the wanted reversed range is reached.

var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    start = 2,
    end = 5,
    result = array.map((v, i, a) => i >= start && i <= end ? a[end - i + start] : v);
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324