Skip to content

ErcanOPAK.com

  • ASP.Net WebForms
  • ASP.Net MVC
  • C#
  • SQL
  • MySQL
  • PHP
  • Devexpress
  • Reportviewer
  • About
JavaScript

Splice() method in Javascript

- 28.09.21 - ErcanOPAK

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, you should use the slice() method.

Syntax:

splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)

Parameters

start
The index at which to start changing the array.

If greater than the length of the array, start will be set to the length of the array. In this case, no element will be deleted but the method will behave as an adding function, adding as many element as item[n*] provided.

If negative, it will begin that many elements from the end of the array. (In this case, the origin -1, meaning -n is the index of the nth last element, and is therefore equivalent to the index of array.length - n.) If start is negative infinity, it will begin from index 0.

 

deleteCount Optional
An integer indicating the number of elements in the array to remove from start.

If deleteCount is omitted, or if its value is equal to or larger than array.length - start (that is, if it is equal to or greater than the number of elements left in the array, starting at start), then all the elements from start to the end of the array will be deleted.

If deleteCount is 0 or negative, no elements are removed. In this case, you should specify at least one new element.

 

item1, item2, ... Optional
The elements to add to the array, beginning from start. If you do not specify any elements, splice() will only remove elements from the array.

Return Value:
An array containing the deleted elements.
If only one element is removed, an array of one element is returned.
If no elements are removed, an empty array is returned.

 

Examples:

let myArray = ['A', 'B', 'C', 'D', 'E']

let removed = myArray.splice(1, 0, 'F')
//1 means: Find 2nd value in array. 
//0 means: Don't delete anything. You will add the 3rd parameter which is 'F' to the place of 2nd value of array (the place of 'C') 
//'F' means: Add this

// myArray is ['A', 'B', 'F', 'C', 'D', 'E']
// removed is [], no elements removed

-------------------------------------------------------------

let myArray = ['A', 'B', 'C', 'D', 'E']
let removed = myArray.splice(4, 0, 'G', 'S')
//1 means: Find 5th value in array. 
//0 means: Don't delete anything. You will add the 3rd and 4th parameters which are 'G' and 'S' to the place of 5th value of array (the place of 'E') 
//'G', 'S' means: Add these

// myArray is ['A', 'B', 'C', 'D', 'G', 'S', 'E']
// return value, removed is [], no elements removed

-------------------------------------------------------------

let myArray = ['A', 'B', 'C', 'D', 'E']
let removed = myArray.splice(3, 1)
//3 means: Find 4th value in array. 
//1 means: Start to delete from 4th value. And delete only 1 value 

// myArray is ['A', 'B', 'C', 'E']
// return value, removed is ['D']

-------------------------------------------------------------

let myArray = ['A', 'B', 'C', 'D', 'E']
let removed = myArray.splice(1, 1, 'F')
//2 means: Find 3rd value in array. 
//1 means: Start to delete from 3rd value. And delete only 1 value
//'F' means: Delete 'B' and add this 'F' instead of it 

// myArray is ['A', 'F', 'C', 'D', 'E']
// return value, removed is ['B']

-------------------------------------------------------------

 

Related posts:

How to disable ASP.Net button after click to prevent double clicking
Add and Remove classes from element in Javascript
Slice() method in Javascript
How to use column search in datatable when responsive is false
Post Views: 4

Post navigation

IndexOf() and LastIndexOf() in C#
Slice() method in Javascript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

September 2023
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  
« Aug    

Most Viewed Posts

  • Get the First and Last Word from a String or Sentence in SQL (665)
  • Get the User Name and Domain Name from an Email Address in SQL (657)
  • How to select distinct rows in a datatable in C# (508)
  • Add Constraint to SQL Table to ensure email contains @ (428)
  • Average of all values in a column that are not zero in SQL (347)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (328)
  • Find numbers with more than two decimal places in SQL (306)
  • Confirm before process with ASPxButton in Devexpress (304)
  • ASPxGridView – Disable CheckBox based on condition in GridViewCommandColumn (277)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (275)

Recent Posts

  • How to remove all non alphanumeric characters from a string in C#
  • How to get the Xth Day of the Week of the Year in C#
  • How to get formatted JSON in C#
  • How to convert JSON to XML or XML to JSON in C#
  • How to use OUTPUT for Insert, Update and Delete in SQL
  • How to get the first and last date of the current year in SQL
  • How to solve extra blank page at end of Microsoft Reportviewer
  • How to Use Picture-in-Picture in Chrome Browser
  • How to add some content to the right side of CardHeader on Bootstrap
  • How to change star rating color on mouseover/out, mouseenter/leave with Javascript

Most Viewed Posts

  • Get the First and Last Word from a String or Sentence in SQL (665)
  • Get the User Name and Domain Name from an Email Address in SQL (657)
  • How to select distinct rows in a datatable in C# (508)
  • Add Constraint to SQL Table to ensure email contains @ (428)
  • Average of all values in a column that are not zero in SQL (347)

Recent Posts

  • How to remove all non alphanumeric characters from a string in C#
  • How to get the Xth Day of the Week of the Year in C#
  • How to get formatted JSON in C#
  • How to convert JSON to XML or XML to JSON in C#
  • How to use OUTPUT for Insert, Update and Delete in SQL

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter

© 2023 ErcanOPAK.com

Proudly powered by WordPress | Theme: Xblog Plus by wpthemespace.com