JavaScript Objects Functions and Arrays Explained by Tony de Araujo

JavaScript Objects Functions and Arrays Explained by Tony de Araujo

Author:Tony de Araujo [de Araujo, Tony]
Language: eng
Format: epub, pdf
Publisher: Tony de Araujo
Published: 2014-05-30T22:00:00+00:00


2.2.8 splice( )

The Array method splice( ) is a very interesting one. Do you still remember push and unshift? they add items to the end or beginning of an array, right? splice() adds items to the middle ( or anywhere in the array).

Let’s see how it works.

Depending on how many arguments you use in the parenthesis, you can the following:

a) Delete items mode:

When you use only two arguments you are giving the index positions of

First argument: where the action is to start (inclusively),

Second argument: How many items will be removed.

Two arguments means deleting because there is nothing to add.

b) Add items mode,

If you insert more than two arguments, the third and consecutive arguments are data to be inserted, and in that case JavaScript assumes you want to insert items into the array.

In juts add mode, the second argument is zero, which means that no items will be deleted in exchange for the items we are adding with the third and consecutive parameters.

c) Replace items mode.

If the second parameter in add mode is anything other than zero, this number will indicate how many items will be deleted in exchange for the items being added.

For example splice(0,3) means to delete 3 items: the first, second and third. Number 3 represents the quantity of items to be removed. The number 3 coincides with the third item on the array but it is not necessarily so. If we had splice(2,3) it would remove the third, fourth and fifth items, which correspond to positions 2,3,4 and counting from zero.

The 3rd and consecutive arguments represent the items to be inserted.

For example, splice(2,0, "yellow") means to insert yellow in position two (third item) and unshift the others because the zero means that no items are to be deleted.

In splice(2,1, "yellow") it means to add yellow to position 2 ( third item) and remove whatever is there (this is a replacement due to the second argument).

The picture below will illustrate the concept:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Deep Learning with Python by François Chollet(12555)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7771)
Grails in Action by Glen Smith Peter Ledbrook(7685)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6398)
Kotlin in Action by Dmitry Jemerov(5048)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3752)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3326)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(3084)
Mastering Bitcoin: Programming the Open Blockchain by Andreas M. Antonopoulos(2863)
The Art Of Deception by Kevin Mitnick(2602)
Drugs Unlimited by Mike Power(2465)
Kali Linux - An Ethical Hacker's Cookbook: End-to-end penetration testing solutions by Sharma Himanshu(2310)
The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution by Walter Isaacson(2292)
Writing for the Web: Creating Compelling Web Content Using Words, Pictures and Sound (Eva Spring's Library) by Lynda Felder(2261)
SEO 2018: Learn search engine optimization with smart internet marketing strategies by Adam Clarke(2190)
JavaScript by Example by S Dani Akash(2134)
A Blueprint for Production-Ready Web Applications: Leverage industry best practices to create complete web apps with Python, TypeScript, and AWS by Dr. Philip Jones(2084)
DarkMarket by Misha Glenny(2083)
Wireless Hacking 101 by Karina Astudillo(2075)
Full-Stack React Projects by Shama Hoque(1990)