SlideShare une entreprise Scribd logo
1  sur  46
Télécharger pour lire hors ligne
Arrays
Arrays
Declaring and Allocating Array
Types of Array
Array Methods
TOPICS
Arrays
Array inherits from Object.
Indexes are converted to strings and used as names for
retrieving values.
Not very efficient in most other cases.
The JavaScript Array object is a global object that is used
in the construction of arrays; which are high-level, list-like
objects.
One advantage: No need to provide a length or type
when creating an array.
Normally, arrays allocate a contiguous block of memory of
fixed length. However, in Javascript, arrays are:
Object types with special constructors and accessor
methods.
Which means, a statement like:
var arr = new Array(100000);
does not allocate any memory! In fact, it simply sets the value
of the length property in the array.
When you construct an array, you don't need to declare a
size as they grow automatically.
So, you should use this instead:
var arr = [];
Arrays in Javascript are sparse which means not all
the elements in the array may contain data. In
other words, only the elements that actually contain
data exist in the array. This reduces the amount of
memory used by the array. The values are located
by a key and not by an offset. They're simply a
method of convenience and not intended to be
used for complex numerical analysis.
Arrays in Javascript are not typed so the value of an
element can be an object, string, number, boolean,
function or an array.
Declaring and Allocating Arrays
JavaScript arrays are Array objects.
Creating new objects using the new
operator is known as creating an instance
or instantiating an object
Operator new is known as the dynamic
memory allocation operator
Declare + Initialize Arrays
Only declaration
var arr=[];
Using the conventional Syntax
var arr = [“a” , ”b” , ”c”];
Using the JavaScript Keyword new
var arr = new Array(“1”,”2”,”3”,”4”);
document.write(arr[0]+arr[1]+arr[2]+arr[3]);
Types of Array
Associative Array
Index Array
Associative Array
//Associative array example
var person ={ firstName: "Frayosh", Lastname:"Wadia“ };
document.write(""+person["firstName"]+
“ ”+person["Lastname"] );
Javascript associative array is a type of array which
stores data using name value pairs
The array data can be accessed by specifying the key rather
than the index
Output : Frayosh Wadia
Indexing Array
//Indexing Array
<script>
var ary = ["A", "B" , "C" , "D" , "E","F"];
document.write(“<br>"+ary[4]);
<script>
Output : E
USING LOOP
//Using Loop and iterating the array
<script>
var arr=new Array(“1”,”2”,”3”,”4”);
for(i=0;i<ary.length;i++)
{
document.write(ary[i]);
}
</script>
Output : 1234
Array Methods
 Concat
 Join
 Push
 Pop
 Unshift
 Shift
 Sort
 Reverse
 Slice
 Splice
 IndexOf
 LastIndexOf
 Length
CONCAT
Javascript array concat() method returns a new
array comprised of this array joined with two or
more arrays.
Syntax:
The syntax of concat() method is as follows −
array.concat(value1, value2, ..., valueN);
Return Value:
Returns the length of the array.
//Array method Concat
<script>
var a=new Array("Hello");
var b=new Array("World");
document.write("<br>"+a.concat(b));
</script>
Output :Hello,World
Join
Javascript array join() method joins all the elements
of an array into a string.
Syntax
Its syntax is as follows −
array.join(separator);
Parameter Details
separator − Specifies a string to separate each
element of the array.
Return Value
Returns a string after joining all the array elements.
//Array Method: Join
<script>
var c=new Array(6,7,8,9);
document.write("<br>"+c.join("/"));
</script>
Output : 6/7/8/9
PUSH
Javascript array push() method appends the given
element(s) in the last of the array and returns the
length of the new array.
Syntax
Its syntax is as follows −
array.push(element1, ..., elementN);
Parameter Details
element1, ..., elementN: The elements to add to the
end of the array.
Return Value
Returns the length of the new array.
// Array Method Push
<script>
var d=new Array("Frayosh","Lalit","Sameer");
d.push("Muzzamil");
document.write("<br>"+d);
</script>
Output :Frayosh,Lalit,Sameer,Muzzamil
POP
Javascript array pop() method removes
the last element from an array and returns
that element.
Syntax
Its syntax is as follows −
array.pop();
Return Value
Returns the removed element from the array.
// Array method pop
<script>
var e=new Array(1,2,3,4);
e.pop();
document.write("<br>"+e);
</script>
Output :1,2,3
UNSHIFT
Javascript array unshift() method adds one or
more elements to the beginning of an array and
returns the new length of the array.
Syntax
Its syntax is as follows −
array.unshift( element1, ..., elementN );
Parameter Details
element1, ..., elementN − The elements to add to
the front of the array.
Return Value
Returns the length of the new array.
// Array method: Unshift
<script>
var f=new Array("Frayosh","Lalit","Sameer");
f.unshift("Muzzamil");
document.write("<br>"+f);
</script>
Output :Muzzamil,Frayosh,Lalit,Sameer
SHIFT
Javascript array shift()method removes the first
element from an array and returns that
element.
Syntax
Its syntax is as follows −
array.shift();
Return Value
Returns the removed single value of the array.
// Array Method: Shift
<script>
var g=new Array(1,2,3,4);
g.shift();
document.write("<br>"+g);
</script>
Output : 2,3,4
SORT
Javascript array sort() method sorts the elements of
an array.
Syntax
Its syntax is as follows −
array.sort();
Return Value
Returns a sorted array.
//Array Method:Sort
<script>
var h=new
Array("Frayosh","Lalit","Sameer","Muzzamil");
h.sort();
document.write("<br>"+h);
</script>
Output : Frayosh,Lalit,Muzzamil,Sameer
REVERSE
Javascript array reverse() method reverses the
element of an array. The first array element
becomes the last and the last becomes the first.
Syntax
Its syntax is as follows −
array.reverse();
Return Value
Returns the reversed single value of the array.
// Array method: Reverse
<script>
var i=new Array(4,7,2,1);
document.write("<br>"+i.reverse());
</script>
Output :1,2,7,4
SLICE
Javascript array slice() method extracts a section of an
array and returns a new array.
Syntax
Its syntax is as follows −
array.slice( begin ,end);
Parameter Details
begin − Zero-based index at which to begin extraction
end − Zero-based index at which to end extraction.
Return Value
Returns the extracted array based on the passed parameters.
//Array Method: Slice
<script>
var k=new Array("A","B","C","D","E");
document.write("<br>"+j.slice(1,4));
<script>
Output :B,C,D
//Array Method:Slice
<script>
var j=new Array("A","B","C","D","E");
document.write("<br>"+j.slice(2));
<script>
Output : C,D,E
Javascript array splice() method changes the content of an array,
adding new elements while removing old elements.
Syntax
Its syntax is as follows −
array.splice(index, howMany, [element1][, ..., elementN]);
Parameter Details
index − Index at which to start changing the array.
howMany − An integer indicating the number of old array elements
to remove.
element1, ..., elementN − The elements to add to the array.
Return Value
Returns the extracted array based on the passed parameters.
Splice
Remove Values
//Splice method used for removing elements
var j=new Array("A","B","C","D","E");
j.splice(2,2);
document.write("<br>"+j);
Output :A,B,E
//Array method :Splice
var l=new Array("A","B","C","D","E");
j.splice(2,0,"S","H");
document.write("<br>"+j);
Add Values
Output : A,B,S,H,C,D,E
ADD & REMOVE
//Splice method used for adding and removing
elements
var j=new Array("A","B","C","D","E");
j.splice(2,2,"S","H");
document.write(“<br>"+j);
Output : A,B,S,H,E
IndexOf
Javascript array indexOf() method returns the first index at
which a given element can be found in the array, or -1 if it is
not present.
Syntax
Its syntax is as follows −
array.indexOf(searchElement, fromIndex);
Parameter Details
searchElement − Element to locate in the array.
fromIndex − The index at which to begin the search.
Defaults to 0, i.e. the whole array will be searched.
Return Value
Returns the index of the found element.
// Array Method: IndexOf
<script>
var ar=new Array("A","B","C","D","E","F","G");
document.write("<br>"+ar.indexOf("F"));
</script>
Output :5
Javascript array lastIndexOf() method returns the last index at
which a given element can be found in the array, or -1 if it is not
present. The array is searched backwards, starting at fromIndex.
Syntax
Its syntax is as follows −
array.lastIndexOf(searchElement, fromIndex);
Parameter Details
searchElement − Element to locate in the array.
fromIndex − The index at which to start searching backwards.
Defaults to the array's length, i.e., the whole array will be
searched.
Return Value
Returns the index of the found element from the last.
LastIndexOf
// Array method :Last Index Of
<script>
var index = [8, 5, 8, 130, 44,8,16,15];
document.write("<br>"+ index.lastIndexOf(8,6));
</script>
Output :5
LENGTH
Javascript array length property returns an unsigned,
32-bit integer that specifies the number of elements
in an array.
Syntax
Its syntax is as follows −
array.length
Return Value
Returns the length of the array
// Array length returning the length of the
array
<script>
var ar=new Array("A","B","C","D","E","F","G");
document.write("<br>"+ar.length);
</script>
Output :7
Adding Elements
Javascript allows you to declare and empty array and add values later on
Eg. var cities=[];
cities[0]="Delhi";
cities[1]="Mumbai";
cities[2]="Chennai";
cities[3]="Bangalore";
or
You can simply add values during declaration
Eg. var cities=[“Delhi",“Mumbai",“Chennai",“Bangalore"];
or
Create an Array instance(new) and add values
Eg. var cities=new Array(“Delhi”,”Mumbai”,”Chennai”,”Bangalore”);
Deleting Elements
delete array[number]
Removes the element, but leaves a hole in the numbering.
Returns Boolean value
array.splice(number, 1)
Removes the element and renumbers all the following
elements. Returns the deleted value.
Deleting Elements
//deleting Elements using delete
var cities=["Delhi","Mumbai","Chennai","Bangalore"];
delete cities[1];
document.write("<br>Array after using delete :"+ cities);
document.write("<br>Displaying element at 1st position:"+cities[1]);
Output
Array after using delete :Delhi,,Chennai,Bangalore
Displaying element at 1st position:undefined
//deleting using splice
<script>
var cities=["Delhi","Mumbai","Chennai","Bangalore"];
document.write("<br>Deleted Element:"+cities.splice(1,1));
document.write("<br>Array after using splice:"+ cities);
document.write("<br>Displaying element at 1st position:"+
cities[1]);
</script>
Output
Deleted Element:Mumbai
Array after using splice:Delhi,Chennai,Bangalore
Displaying element at 1st position:Chennai
THANKS
FOR
WATCHING

Contenu connexe

Tendances

Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaEdureka!
 
Extractors & Implicit conversions
Extractors & Implicit conversionsExtractors & Implicit conversions
Extractors & Implicit conversionsKnoldus Inc.
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java languageHareem Naz
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 
Introduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in HaskellIntroduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in Haskellnebuta
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsFrançois Garillot
 
Arrays in python
Arrays in pythonArrays in python
Arrays in pythonmoazamali28
 

Tendances (20)

Knolx session
Knolx sessionKnolx session
Knolx session
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
Array lecture
Array lectureArray lecture
Array lecture
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Python array
Python arrayPython array
Python array
 
Data Structure (Static Array)
Data Structure (Static Array)Data Structure (Static Array)
Data Structure (Static Array)
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Algorithm and Programming (Array)
Algorithm and Programming (Array)Algorithm and Programming (Array)
Algorithm and Programming (Array)
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Extractors & Implicit conversions
Extractors & Implicit conversionsExtractors & Implicit conversions
Extractors & Implicit conversions
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Introduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in HaskellIntroduction to ad-3.4, an automatic differentiation library in Haskell
Introduction to ad-3.4, an automatic differentiation library in Haskell
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Python data handling notes
Python data handling notesPython data handling notes
Python data handling notes
 
02 arrays
02 arrays02 arrays
02 arrays
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on Steroids
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 

En vedette

Didaktiske Modellen
Didaktiske ModellenDidaktiske Modellen
Didaktiske Modellentoberg
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaDipayan Sarkar
 
Parallel Processors (SIMD)
Parallel Processors (SIMD) Parallel Processors (SIMD)
Parallel Processors (SIMD) Ali Raza
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queuesurya pandian
 
Parallel computing
Parallel computingParallel computing
Parallel computingvirend111
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueGhaffar Khan
 
List Data Structure
List Data StructureList Data Structure
List Data StructureZidny Nafan
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data StructuresAmrinder Arora
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)EngineerBabu
 

En vedette (12)

Didaktiske Modellen
Didaktiske ModellenDidaktiske Modellen
Didaktiske Modellen
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Parallel Processors (SIMD)
Parallel Processors (SIMD) Parallel Processors (SIMD)
Parallel Processors (SIMD)
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
 
Parallel computing
Parallel computingParallel computing
Parallel computing
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, Queue
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
Parallel processing Concepts
Parallel processing ConceptsParallel processing Concepts
Parallel processing Concepts
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
Arrays
ArraysArrays
Arrays
 
Array in C
Array in CArray in C
Array in C
 

Similaire à Java script arrays

9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06Terry Yoast
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useMukesh Tekwani
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web ProgrammingAmirul Azhar
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Dhivyaa C.R
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfinfo309708
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvLecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvZahouAmel1
 
Chapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsChapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsDr. Ahmed Al Zaidy
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & AnswersRatnala Charan kumar
 
javascript-Array.ppsx
javascript-Array.ppsxjavascript-Array.ppsx
javascript-Array.ppsxVedantSaraf9
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 
tutorial 10 Exploring Arrays, Loops, and conditional statements.ppt
tutorial 10 Exploring Arrays, Loops, and conditional statements.ppttutorial 10 Exploring Arrays, Loops, and conditional statements.ppt
tutorial 10 Exploring Arrays, Loops, and conditional statements.pptAbdisamedAdam
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objectsPhúc Đỗ
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 

Similaire à Java script arrays (20)

Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
 
Array properties
Array propertiesArray properties
Array properties
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and use
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
 
UNIT IV (4).pptx
UNIT IV (4).pptxUNIT IV (4).pptx
UNIT IV (4).pptx
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvLecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
 
Chapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsChapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statements
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
javascript-Array.ppsx
javascript-Array.ppsxjavascript-Array.ppsx
javascript-Array.ppsx
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
22.ppt
22.ppt22.ppt
22.ppt
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
tutorial 10 Exploring Arrays, Loops, and conditional statements.ppt
tutorial 10 Exploring Arrays, Loops, and conditional statements.ppttutorial 10 Exploring Arrays, Loops, and conditional statements.ppt
tutorial 10 Exploring Arrays, Loops, and conditional statements.ppt
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
 
Php array
Php arrayPhp array
Php array
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 

Dernier

Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineCeline George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...Nguyen Thanh Tu Collection
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroomSamsung Business USA
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptxmary850239
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 

Dernier (20)

Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command Line
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 

Java script arrays

  • 2. Arrays Declaring and Allocating Array Types of Array Array Methods TOPICS
  • 3. Arrays Array inherits from Object. Indexes are converted to strings and used as names for retrieving values. Not very efficient in most other cases. The JavaScript Array object is a global object that is used in the construction of arrays; which are high-level, list-like objects. One advantage: No need to provide a length or type when creating an array.
  • 4. Normally, arrays allocate a contiguous block of memory of fixed length. However, in Javascript, arrays are: Object types with special constructors and accessor methods. Which means, a statement like: var arr = new Array(100000); does not allocate any memory! In fact, it simply sets the value of the length property in the array. When you construct an array, you don't need to declare a size as they grow automatically.
  • 5. So, you should use this instead: var arr = []; Arrays in Javascript are sparse which means not all the elements in the array may contain data. In other words, only the elements that actually contain data exist in the array. This reduces the amount of memory used by the array. The values are located by a key and not by an offset. They're simply a method of convenience and not intended to be used for complex numerical analysis. Arrays in Javascript are not typed so the value of an element can be an object, string, number, boolean, function or an array.
  • 6. Declaring and Allocating Arrays JavaScript arrays are Array objects. Creating new objects using the new operator is known as creating an instance or instantiating an object Operator new is known as the dynamic memory allocation operator
  • 7. Declare + Initialize Arrays Only declaration var arr=[]; Using the conventional Syntax var arr = [“a” , ”b” , ”c”]; Using the JavaScript Keyword new var arr = new Array(“1”,”2”,”3”,”4”); document.write(arr[0]+arr[1]+arr[2]+arr[3]);
  • 8. Types of Array Associative Array Index Array
  • 9. Associative Array //Associative array example var person ={ firstName: "Frayosh", Lastname:"Wadia“ }; document.write(""+person["firstName"]+ “ ”+person["Lastname"] ); Javascript associative array is a type of array which stores data using name value pairs The array data can be accessed by specifying the key rather than the index Output : Frayosh Wadia
  • 10. Indexing Array //Indexing Array <script> var ary = ["A", "B" , "C" , "D" , "E","F"]; document.write(“<br>"+ary[4]); <script> Output : E
  • 11. USING LOOP //Using Loop and iterating the array <script> var arr=new Array(“1”,”2”,”3”,”4”); for(i=0;i<ary.length;i++) { document.write(ary[i]); } </script> Output : 1234
  • 12. Array Methods  Concat  Join  Push  Pop  Unshift  Shift  Sort  Reverse  Slice  Splice  IndexOf  LastIndexOf  Length
  • 13. CONCAT Javascript array concat() method returns a new array comprised of this array joined with two or more arrays. Syntax: The syntax of concat() method is as follows − array.concat(value1, value2, ..., valueN); Return Value: Returns the length of the array.
  • 14. //Array method Concat <script> var a=new Array("Hello"); var b=new Array("World"); document.write("<br>"+a.concat(b)); </script> Output :Hello,World
  • 15. Join Javascript array join() method joins all the elements of an array into a string. Syntax Its syntax is as follows − array.join(separator); Parameter Details separator − Specifies a string to separate each element of the array. Return Value Returns a string after joining all the array elements.
  • 16. //Array Method: Join <script> var c=new Array(6,7,8,9); document.write("<br>"+c.join("/")); </script> Output : 6/7/8/9
  • 17. PUSH Javascript array push() method appends the given element(s) in the last of the array and returns the length of the new array. Syntax Its syntax is as follows − array.push(element1, ..., elementN); Parameter Details element1, ..., elementN: The elements to add to the end of the array. Return Value Returns the length of the new array.
  • 18. // Array Method Push <script> var d=new Array("Frayosh","Lalit","Sameer"); d.push("Muzzamil"); document.write("<br>"+d); </script> Output :Frayosh,Lalit,Sameer,Muzzamil
  • 19. POP Javascript array pop() method removes the last element from an array and returns that element. Syntax Its syntax is as follows − array.pop(); Return Value Returns the removed element from the array.
  • 20. // Array method pop <script> var e=new Array(1,2,3,4); e.pop(); document.write("<br>"+e); </script> Output :1,2,3
  • 21. UNSHIFT Javascript array unshift() method adds one or more elements to the beginning of an array and returns the new length of the array. Syntax Its syntax is as follows − array.unshift( element1, ..., elementN ); Parameter Details element1, ..., elementN − The elements to add to the front of the array. Return Value Returns the length of the new array.
  • 22. // Array method: Unshift <script> var f=new Array("Frayosh","Lalit","Sameer"); f.unshift("Muzzamil"); document.write("<br>"+f); </script> Output :Muzzamil,Frayosh,Lalit,Sameer
  • 23. SHIFT Javascript array shift()method removes the first element from an array and returns that element. Syntax Its syntax is as follows − array.shift(); Return Value Returns the removed single value of the array.
  • 24. // Array Method: Shift <script> var g=new Array(1,2,3,4); g.shift(); document.write("<br>"+g); </script> Output : 2,3,4
  • 25. SORT Javascript array sort() method sorts the elements of an array. Syntax Its syntax is as follows − array.sort(); Return Value Returns a sorted array.
  • 27. REVERSE Javascript array reverse() method reverses the element of an array. The first array element becomes the last and the last becomes the first. Syntax Its syntax is as follows − array.reverse(); Return Value Returns the reversed single value of the array.
  • 28. // Array method: Reverse <script> var i=new Array(4,7,2,1); document.write("<br>"+i.reverse()); </script> Output :1,2,7,4
  • 29. SLICE Javascript array slice() method extracts a section of an array and returns a new array. Syntax Its syntax is as follows − array.slice( begin ,end); Parameter Details begin − Zero-based index at which to begin extraction end − Zero-based index at which to end extraction. Return Value Returns the extracted array based on the passed parameters.
  • 30. //Array Method: Slice <script> var k=new Array("A","B","C","D","E"); document.write("<br>"+j.slice(1,4)); <script> Output :B,C,D
  • 31. //Array Method:Slice <script> var j=new Array("A","B","C","D","E"); document.write("<br>"+j.slice(2)); <script> Output : C,D,E
  • 32. Javascript array splice() method changes the content of an array, adding new elements while removing old elements. Syntax Its syntax is as follows − array.splice(index, howMany, [element1][, ..., elementN]); Parameter Details index − Index at which to start changing the array. howMany − An integer indicating the number of old array elements to remove. element1, ..., elementN − The elements to add to the array. Return Value Returns the extracted array based on the passed parameters. Splice
  • 33. Remove Values //Splice method used for removing elements var j=new Array("A","B","C","D","E"); j.splice(2,2); document.write("<br>"+j); Output :A,B,E
  • 34. //Array method :Splice var l=new Array("A","B","C","D","E"); j.splice(2,0,"S","H"); document.write("<br>"+j); Add Values Output : A,B,S,H,C,D,E
  • 35. ADD & REMOVE //Splice method used for adding and removing elements var j=new Array("A","B","C","D","E"); j.splice(2,2,"S","H"); document.write(“<br>"+j); Output : A,B,S,H,E
  • 36. IndexOf Javascript array indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present. Syntax Its syntax is as follows − array.indexOf(searchElement, fromIndex); Parameter Details searchElement − Element to locate in the array. fromIndex − The index at which to begin the search. Defaults to 0, i.e. the whole array will be searched. Return Value Returns the index of the found element.
  • 37. // Array Method: IndexOf <script> var ar=new Array("A","B","C","D","E","F","G"); document.write("<br>"+ar.indexOf("F")); </script> Output :5
  • 38. Javascript array lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex. Syntax Its syntax is as follows − array.lastIndexOf(searchElement, fromIndex); Parameter Details searchElement − Element to locate in the array. fromIndex − The index at which to start searching backwards. Defaults to the array's length, i.e., the whole array will be searched. Return Value Returns the index of the found element from the last. LastIndexOf
  • 39. // Array method :Last Index Of <script> var index = [8, 5, 8, 130, 44,8,16,15]; document.write("<br>"+ index.lastIndexOf(8,6)); </script> Output :5
  • 40. LENGTH Javascript array length property returns an unsigned, 32-bit integer that specifies the number of elements in an array. Syntax Its syntax is as follows − array.length Return Value Returns the length of the array
  • 41. // Array length returning the length of the array <script> var ar=new Array("A","B","C","D","E","F","G"); document.write("<br>"+ar.length); </script> Output :7
  • 42. Adding Elements Javascript allows you to declare and empty array and add values later on Eg. var cities=[]; cities[0]="Delhi"; cities[1]="Mumbai"; cities[2]="Chennai"; cities[3]="Bangalore"; or You can simply add values during declaration Eg. var cities=[“Delhi",“Mumbai",“Chennai",“Bangalore"]; or Create an Array instance(new) and add values Eg. var cities=new Array(“Delhi”,”Mumbai”,”Chennai”,”Bangalore”);
  • 43. Deleting Elements delete array[number] Removes the element, but leaves a hole in the numbering. Returns Boolean value array.splice(number, 1) Removes the element and renumbers all the following elements. Returns the deleted value.
  • 44. Deleting Elements //deleting Elements using delete var cities=["Delhi","Mumbai","Chennai","Bangalore"]; delete cities[1]; document.write("<br>Array after using delete :"+ cities); document.write("<br>Displaying element at 1st position:"+cities[1]); Output Array after using delete :Delhi,,Chennai,Bangalore Displaying element at 1st position:undefined
  • 45. //deleting using splice <script> var cities=["Delhi","Mumbai","Chennai","Bangalore"]; document.write("<br>Deleted Element:"+cities.splice(1,1)); document.write("<br>Array after using splice:"+ cities); document.write("<br>Displaying element at 1st position:"+ cities[1]); </script> Output Deleted Element:Mumbai Array after using splice:Delhi,Chennai,Bangalore Displaying element at 1st position:Chennai