Array elements are enclosed in square brackets ([]). Array literals may contain zero or more elements. The values are specified in the array list and its length is set based on the number of arguments specified.
create an Array
var cars = ['BMW','AUDI','BENZ'];
cars[0] // 'BMW' the First element
cars[1] // 'AUDI' the second element
cars[2] //'BENZ' the third element
To find the length of an Array
console.log(cars.length) // 3
Access an Array item
console.log(cars[1]); //AUDI
console.log(cars[cars.length-1]); //BENZ
Looping an Array (forEach)
cars.forEach(function( item ,index ,array ) {
console.log(item , index )
});
Displays all the values from the array with its specific index.
// BMW , 0
// AUDI , 1
// BENZ, 2
To Add the value at the end of the Array
var addingValues = cars.push('SWIFT');
To remove value from the end of the Array
var popLastValue = cars.pop(); // removes SWIFT from the end of the array
// [ BMW, AUDI, BENZ ]
To remove value from the front of the Array
var removeFirstValue = cars.shift(); // remove BMW from the first
// [ AUDI, BENZ ]
To add value from the front of the Array
var addValueAtFirst = cars.unshift('INNOVA');
// [ INNOVA,AUDI,BENZ ]
Find the index of an Array
var findIndex = cars.indexOf('AUDI');
// 1
To remove an item from an index position
var removeItem = cars.splice(findIndex, 1); // To remove the item we have to know its index position and then splice it.
// [ INNOVA,BENZ ]
To remove more than one item from an index position
var chocolates = [ 'mango','kitkat','dairymilk','barone' ];
console.log(chocolates);
// [ "mango","kitkat","dairymilk","barone" ]
var pos = 1, n = 2;
var removedItems = chocolates.splice(pos,n);
Here, the n defined the number of items to be removed from the array and pos defines the start position from an array.
console.log(chocolates) // [ mango , barone ] the values which are left in the array
console.log(removedItems) // [ kitkat, dairymilk ] the values which are been removed from the array.
copy an Array
var copyArray = chocolates.slice(); // we use method slice to copy an array.
// [ mango , barone ]
create an Array
var cars = ['BMW','AUDI','BENZ'];
cars[0] // 'BMW' the First element
cars[1] // 'AUDI' the second element
cars[2] //'BENZ' the third element
To find the length of an Array
console.log(cars.length) // 3
Access an Array item
console.log(cars[1]); //AUDI
console.log(cars[cars.length-1]); //BENZ
Looping an Array (forEach)
cars.forEach(function( item ,index ,array ) {
console.log(item , index )
});
Displays all the values from the array with its specific index.
// BMW , 0
// AUDI , 1
// BENZ, 2
To Add the value at the end of the Array
var addingValues = cars.push('SWIFT');
To remove value from the end of the Array
var popLastValue = cars.pop(); // removes SWIFT from the end of the array
// [ BMW, AUDI, BENZ ]
To remove value from the front of the Array
var removeFirstValue = cars.shift(); // remove BMW from the first
// [ AUDI, BENZ ]
To add value from the front of the Array
var addValueAtFirst = cars.unshift('INNOVA');
// [ INNOVA,AUDI,BENZ ]
Find the index of an Array
var findIndex = cars.indexOf('AUDI');
// 1
To remove an item from an index position
var removeItem = cars.splice(findIndex, 1); // To remove the item we have to know its index position and then splice it.
// [ INNOVA,BENZ ]
To remove more than one item from an index position
var chocolates = [ 'mango','kitkat','dairymilk','barone' ];
console.log(chocolates);
// [ "mango","kitkat","dairymilk","barone" ]
var pos = 1, n = 2;
var removedItems = chocolates.splice(pos,n);
Here, the n defined the number of items to be removed from the array and pos defines the start position from an array.
console.log(chocolates) // [ mango , barone ] the values which are left in the array
console.log(removedItems) // [ kitkat, dairymilk ] the values which are been removed from the array.
copy an Array
var copyArray = chocolates.slice(); // we use method slice to copy an array.
// [ mango , barone ]
No comments:
Post a Comment