# Increment/Decrement an array items index number? \[Answered\]

**URL:** <https://community.weweb.io/t/increment-decrement-an-array-items-index-number-answered/2671>\
**Category:** How do I?\
**Created:** [May 12, 2023, 8:01pm UTC](https://community.weweb.io/t/increment-decrement-an-array-items-index-number-answered/2671 "2023-05-12T20:01:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![twinsticks](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@twinsticks](https://community.weweb.io/u/twinsticks)\
**Post date:** [May 12, 2023, 8:01pm UTC](https://community.weweb.io/t/increment-decrement-an-array-items-index-number-answered/2671/1 "2023-05-12T20:01:48Z")

</div>

I think I might be missing something, but I would like to change the array index of an item in my array.

Is there a way to do this with no code?

---

<div class="post-metadata">

**Author:** ![jaredgibb](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@jaredgibb](https://community.weweb.io/u/jaredgibb)\
**Post date:** [May 13, 2023, 3:52am UTC](https://community.weweb.io/t/increment-decrement-an-array-items-index-number-answered/2671/2 "2023-05-13T03:52:10Z")

</div>

Certainly! Here’s an example of a function in JavaScript that takes an array, a position index, and a desired position index. It returns a new array with the item at the desired position:

javascript  
Copy code  
function moveItemToPosition(array, currentPosition, desiredPosition) {  
// Create a copy of the original array  
const newArray = array.slice();

// Remove the item from the current position and store it in a variable  
const item = newArray.splice(currentPosition, 1)[0];

// Insert the item at the desired position  
newArray.splice(desiredPosition, 0, item);

return newArray;  
}  
Here’s an example usage of the function:

javascript  
Copy code  
const originalArray = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];  
const currentPosition = 2;  
const desiredPosition = 4;

const newArray = moveItemToPosition(originalArray, currentPosition, desiredPosition);  
console.log(newArray); // Output: [‘a’, ‘b’, ‘d’, ‘e’, ‘c’]

In the example above, the item at index 2 (‘c’) is moved to index 4 in the new array. The original array remains unchanged.

---

<div class="post-metadata">

**Author:** ![twinsticks](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@twinsticks](https://community.weweb.io/u/twinsticks)\
**Post date:** [May 13, 2023, 5:52am UTC](https://community.weweb.io/t/increment-decrement-an-array-items-index-number-answered/2671/3 "2023-05-13T05:52:48Z")

</div>

Thanks so much!

I actually got there with a similar approach, I converted this stackoverflow answer into a custom JS function in weWeb:

```auto
var data = [1,2,3,4,5];

function moveItem(from, to) {
  // remove `from` item and store it
  var f = data.splice(from, 1)[0];
  // insert stored item into position `to`
  data.splice(to, 0, f);
}

moveItem(0, 2);

```

All I had to do was set data = my xano array and change the function attributes to two of my current collection items.

Thanks again
