Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

change array index position in javascript by up and down click

// move up by 1 postion in Array
const moveUp=(id)=> {
  let index = arr.findIndex(e => e.id == id);
  if (index > 0) {
    let el = arr[index];
    arr[index] = arr[index - 1];
    arr[index - 1] = el;
  }
}

// move dwon by 1 postion in Array
const moveDown=(id)=> {
  let index = arr.findIndex(e => e.id == id);
  if (index !== -1 && index < arr.length - 1) {
    let el = arr[index];
    arr[index] = arr[index + 1];
    arr[index + 1] = el;
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Expected the depth of nested jsx elements to be <= 2, but found 3 
Javascript :: set embed color discord.js 
Javascript :: dull a background image in react native 
Javascript :: js start at this character 
Javascript :: array.push 
Javascript :: check if element with class has another class javascript 
Javascript :: range of numbers in javascript 
Javascript :: js environment variables 
Javascript :: javascript charcode 
Javascript :: django ajax redirect to a view on success 
Javascript :: javascript prototype inheritance example 
Javascript :: responseText js 
Javascript :: dot geometru three js 
Javascript :: how to remove first element from array in javascript 
Javascript :: send data from servlet to hjsp 
Javascript :: javascript regex One or more occurrences of the pattern 
Javascript :: jquery add 
Javascript :: js get class from instance 
Javascript :: kafka nodejs example 
Javascript :: javascript merge two array with spread operator 
Javascript :: react video 
Javascript :: Initialize Axios React Redux CRUD API calls 
Javascript :: start animation with javascript 
Javascript :: js get current seconds 
Javascript :: how to check if a key is present in a dictionary in js 
Javascript :: what regular expression will match valid international phone numbers 
Javascript :: Max JavaScript Methods 
Javascript :: how to run cypress test 
Javascript :: javascript reflection 
Javascript :: how to do jest unit test in react 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =