ARRAYS
Array stores multiple values in single variable.
Collections of similar data type Stored in Continuous Memory
Locations.
Syntax
var fruit = new Array( "graphs",
"orange", "mango" );
fruit[0]
is the first element
fruit[1]
is the second element
fruit[2]
is the third element
let cities=["madurai","theni","chennai"];
document.write(cities)
console.log(cities)
console.log(cities[4])
Output
Multidimensional Array
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<h2 style="background-color: rgb(9, 238, 9);color:rgb(255, 0,
0);font-weight: bolder;font-family: Georgia, 'Times New Roman', Times,
serif;">Array</h2>
<h5 style="color: rgb(231, 15, 166);">Output</h5>
<script type="text/javascript" src="basic.js"></script>
</head>
<body>
</body>
</html>
let matrix=[[1,2,3],[4,5,6],[7,8,9]]
var a="<br>";
for(let i=0;i<3;i++)
{
for(let j=0;j<3;j++)
{
document.write(" "+matrix[i][j]);
}
document.write(a);
}
Output
ARRAY OBJECT METHOD
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<h2 style="background-color: rgb(9, 238, 9);color:rgb(255, 0,
0);font-weight: bolder;font-family: Georgia, 'Times New Roman', Times,
serif;">Array</h2>
<h5 style="color: rgb(231, 15, 166);">Output</h5>
<script type="text/javascript" src="basic.js"></script>
</head>
<body>
</body>
</html>
var arr
= new Array( 10, 20,
30 );
document.write("<h3>Array
Length</h3>");
document.write("arr.length
is:" + arr.length);
var alpha = ["a", "b", "c"];
var numeric = [1,
2, 3];
var alphaNumeric = alpha.concat(numeric);
document.write("<br>");
document.write("<h3>Array Concatenation</h3>");
document.write("<br>");
document.write("alphaNumeric : " + alphaNumeric );
Output
let a=['a','b','c','d']
var b="<br>";
document.write("<h3>Array</h3>");
document.write(a);
a.push('f')
document.write(b);
document.write("<h3>Array
Object Push</h3>");
document.write(a);
a.pop()
document.write(b);
document.write("<h3>Array
Object Pop</h3>");
document.write(a);
a.shift()
document.write(b);
document.write("<h3>Array
Shift</h3>");
document.write(a);
a.unshift('a')
document.write(b);
document.write("<h3>Array
Unshift</h3>");
document.write(a);
delete a[2]
document.write(b);
document.write("<h3>Array
Delete</h3>");
document.write(a);
let c=['a','b','c','d','e','f']
c.splice(2,1)
document.write(b);
document.write("<h3>Array
Splice index 2 is 'c' so C will delete</h3>");
document.write(c);
c.splice(1,1,'n')
document.write(b);
document.write("<h3>Array
Splice Reassign index 1 is b->n</h3>");
document.write(c);
let str="x,y,z"
let arr=str.split(',')
document.write(b);
document.write(arr);
arr.reverse()
document.write(b);
document.write("<h3>Array
Reverse</h3>");
document.write(arr);
let fa1=[5,6,7]
let fa2=[9,4,2]
let total=[fa1+","+fa2]
let total1=[fa1+fa2]
console.log(total)
document.write(b);
document.write("<h3>Array
Concatenation</h3>");
document.write(total);
Output
Exercise
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function test()
{
var myarr=[54,67,23,34,56,67];
console.log('Array',myarr);
myarr.pop();
console.log('After pop',myarr);
myarr.shift();
console.log('After Shift',myarr);
myarr.unshift(82);
console.log('After UnShift',myarr);
myarr.push(100);
console.log('Push',myarr);
var pos=myarr.indexOf(23);
console.log(pos);
var x=myarr.slice(2,4);
console.log(x);
console.log(myarr);
}
</script>
</head>
<body>
<button onclick="test()">Test Functions</button>
</body>
</html>
Output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function test()
{
var myarr=[54,67,23,324,516,67,'apple','GUAVA'];
console.log('Array',myarr);
myarr.sort();
console.log('sorted ..',myarr);
var myarr=['apple','orange','grapes','guava','pineapple','pomegranate'];
myarr.sort();
console.log(myarr);
}
let sortme=function()
{
var myarr=[54,67,23,324,516,67];
console.log(myarr);
myarr.sort(comparenum);
console.log(myarr);
};
function comparenum(a,b)
{
return a-b;
}
</script>
</head>
<body>
<button onclick="test()">Test Functions</button>
<button onclick="sortme()">Test Functions</button>
</body>
</html>
Output
0 comments:
Post a Comment