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:

Add and Remove classes from element in Javascript
How to get session value in Javascript
Smart way to truncate long strings in Javascript
Pass model from Razor view to JavaScript
Post Views: 6

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 *

October 2024
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
28293031  
« Sep    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (848)
  • Get the First and Last Word from a String or Sentence in SQL (756)
  • How to select distinct rows in a datatable in C# (704)
  • How to add default value for Entity Framework migrations for DateTime and Bool (584)
  • Add Constraint to SQL Table to ensure email contains @ (521)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (480)
  • How to make theater mode the default for Youtube (465)
  • Average of all values in a column that are not zero in SQL (453)
  • Find numbers with more than two decimal places in SQL (383)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (371)

Recent Posts

  • How to Reset Taskbar in Windows 11
  • Essential Steps to Take After Windows 11 Updates
  • How to list all tables referencing a table by Foreign Key in MS SQL
  • How to format date in Javascript
  • How to generate a random number for each row in T-SQL
  • How to solve ‘Microsoft.TeamFoundation.Git.Contracts.GitCheckoutConflictException’ problem
  • Why nautical mile equals 1852 mt
  • How to Find Day Name From Date in SQL Server
  • How to make pagination in MS SQL Server
  • How to update Identity Column in SQL Server

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (848)
  • Get the First and Last Word from a String or Sentence in SQL (756)
  • How to select distinct rows in a datatable in C# (704)
  • How to add default value for Entity Framework migrations for DateTime and Bool (584)
  • Add Constraint to SQL Table to ensure email contains @ (521)

Recent Posts

  • How to Reset Taskbar in Windows 11
  • Essential Steps to Take After Windows 11 Updates
  • How to list all tables referencing a table by Foreign Key in MS SQL
  • How to format date in Javascript
  • How to generate a random number for each row in T-SQL

Social

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

© 2024 ErcanOPAK.com

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