Move item in list items on top javascript năm 2024

I was working on a project when I faced an unprecedented and obvious issue. How do I suppose to move an element in an array form one position to another?

My goal is to move an element in index-0 to index-2. Something like this:

const input = ["a", "b", "c"];
const expected = ["b", "c", "a"];

Enter fullscreen mode Exit fullscreen mode

The simplest way, using splice() which gives me the ability to add and remove elements in an array.

First, let's delete element in index-0:

function move(input, from) {
  const numberOfDeletedElm = 1;
  // delete one element only, in index-from
  const arrDeletedElem = input.splice(from, numberOfDeletedElm);
  // ["a"]=["a", "b", "c"].splice(0, 1);
  // and input array is ["b", "c"]
}

Enter fullscreen mode Exit fullscreen mode

But, I don't need an array, I only need the content of the arrDeletedElem.

const elm = input.splice(from, numberOfDeletedElm)[0];

Enter fullscreen mode Exit fullscreen mode

Now, let's add

function move(input, from) {
  const numberOfDeletedElm = 1;
  // delete one element only, in index-from
  const arrDeletedElem = input.splice(from, numberOfDeletedElm);
  // ["a"]=["a", "b", "c"].splice(0, 1);
  // and input array is ["b", "c"]
}

0 to index-2

const numberOfDeletedElm = 0;
input.splice(2, numberOfDeletedElm, elm);

Enter fullscreen mode Exit fullscreen mode

And our

function move(input, from) {
  const numberOfDeletedElm = 1;
  // delete one element only, in index-from
  const arrDeletedElem = input.splice(from, numberOfDeletedElm);
  // ["a"]=["a", "b", "c"].splice(0, 1);
  // and input array is ["b", "c"]
}

1 function well be:

function move(input, from, to) {
  let numberOfDeletedElm = 1;
  const elm = input.splice(from, numberOfDeletedElm)[0];
  numberOfDeletedElm = 0;
  input.splice(to, numberOfDeletedElm, elm);
}
// move(["a", "b", "c"], 0, 2) >> ["b", "c", "a"]

Enter fullscreen mode Exit fullscreen mode

Of course, this can go deeper, that's why I created move-position. Which contains utility functions for moving index in an array.

This vanilla.js version isn’t much longer…

oranges pears apples peaches strawberries

<

script type=”text/javascript”>

var list = document.getElementById(“fruits”).childNodes;

for (var i = 0; i < list.length; i++) { list[i].addEventListener(“click”, function() { fruits.insertBefore(this, fruits.childNodes[0]) }); }

How to add item to top of list in JavaScript?

The unshift() method is a built-in function in JavaScript that allows you to add one or more elements to the beginning of an array.

How do you move an element to the top of an array in JavaScript?

The unshift() method of Array instances adds the specified elements to the beginning of an array and returns the new length of the array.

How do you move items to the front of an array?

The unshift() method adds new elements to the beginning of an array. The unshift() method overwrites the original array.

How do you push to the front of a list in JavaScript?

How to push to the start of an array with the unshift() method. In JavaScript, you use the unshift() method to add one or more elements to the beginning of an array and it returns the array's length after the new elements have been added.