1

Is there a better approach to my code below. my code is working, but I'm just wondering if there is a better way to it. I have an array and i want to return the last item in this array to the first spot.

const replce = arr => {
       let n = arr.pop();
       arr.splice(0, 0, n);
       return arr;
    };

console.log(replce(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']));
Mamun
  • 66,969
  • 9
  • 47
  • 59
Th1
  • 269
  • 2
  • 12
  • 1
    What do you mean by _"better"_? _"my code is working"_ see [Code Review](https://codereview.stackexchange.com/) – guest271314 Feb 11 '19 at 06:21
  • instead of `splice` you can use `unshift`. answers from this [post](https://stackoverflow.com/questions/8073673/how-can-i-add-new-array-elements-at-the-beginning-of-an-array-in-javascript) will help you. – Partho63 Feb 11 '19 at 06:27

7 Answers7

3

You can use Pop with destructing.

let arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
let last = arr.pop()
let final = [last,...arr]

console.log(final)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
2

const replce = arr => {
   return arr.unshift(arr.pop()) && arr;
};

console.log(replce(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']));

Short and sweet.

Partho63
  • 3,117
  • 2
  • 21
  • 39
manish kumar
  • 4,412
  • 4
  • 34
  • 51
  • Your usage of `&&` is confusing. The pop/unshift part is completely unrelated to return, you're just pretending that this is a oneliner at the cost of readability. – gronostaj Feb 11 '19 at 07:01
  • the logic is simple. also `arr.unshift(arr.pop())` it returns the element put in the front. And when we know we have the array at the front , we are returning the `array` after modification – manish kumar Feb 11 '19 at 07:14
  • the logic may be simple, but the way you put it is confusing. That's pointless obfuscation. You could put pop/unshift on a separate line and it would become much cleaner. Making oneliners at the cost of readability is not a virtue. – gronostaj Feb 11 '19 at 07:28
1

Two variables values can be swapped in one destructuring expression. Please refers to doc Destructuring assignment

var arr = ['a', 'b', 'c', 'd','e','f','g','h'];
[arr[0], arr[arr.length-1]] = [arr[arr.length-1], arr[0]];
console.log(arr);
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
0

You can use unshift() with pop() Refer this. Unshift adds an element to the first index in the array

const replce = arr => {
   let n = arr.pop();
   arr.unshift(n)
   return arr;
};
console.log(replce(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']));
ellipsis
  • 12,049
  • 2
  • 17
  • 33
0

unshift() will add to the start of an array, and pop() will remove from the end of the array. Example below will show you how to do this.

const replace = arr =>{
 let tempvalue = arr.pop();
 arr.unshift(tempvalue);
 return arr;
}
console.log(replace(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']));

Not sure if I would call it better as I have not tested for speed, but it is easier to read.

Spangle
  • 762
  • 1
  • 5
  • 14
0

 let arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
    console.log(arr);
    arr = arr.reverse();
    console.log(arr);
0

You can also use this logic to remove last item from array and place the item to first spot.

   const replace = arr => {
       arr.unshift(arr.pop());
       return arr;
  };
  replace(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']);