Arrays

Hi, I’m Divyanshi, a who loves building products. I’d love to combine my passion for learning with my software development skills to continue building personalized learning products for people.I am systematically organized, meticulous, diligent and conscientious self-starter, able to prioritize, plan and strategize effectively to accomplish multiple tasks and stay calm under pressure.With my prior knowledge and experience of project management and timed-completion, I move forward to discover and learn diversified challenges in the field of technology that just makes me learn more. Due to the diversity of our clients which included technology corporation,medical industry,foundations,I learn to adapt to changing environments,quickly learning new content & collaborate with people with different communication skills. Skills:Html, Css, Jquery, Javascript, Express, Node.js, Webpack, Basic AEM Fundamentals
Arrays are a data structure that can hold multiple data values. The values we store in an array are ordered. Arrays will start at zero instead of 1. An array is useful because it stores multiple values into a single, organized data structure. You can define a new array by listing values separated with commas between square brackets [].
// creates a `mixedData` array with mixed data types
var mixedData = ["abcd", 1, true, undefined, null, "all the things"];
// creates a `arraysInArrays` array with three arrays
var arraysInArrays = [[1, 2, 3], ["Julia", "James"], [true, false, true, false]];
Indexing
Remember that elements in an array are indexed starting at the position 0. To access an element in an array, use the name of the array immediately followed by square brackets containing the index of the value you want to access.
var donuts = ["glazed", "powdered", "sprinkled"];
console.log(donuts[0]);
Prints: "glazed"
console.log(donuts[3]); // the fourth element in `donuts` array does not exist!
Prints: undefined
Arrays has many properties and methods that make them powerful data structures. Properties as special pieces of information about a data structure. Ex-length. Useful for looping over an array. Both strings and arrays use length property. Arrays are special kind of object.
var mixedData = ["abcd", 1, true, undefined, null, "all the things"];
console.log(mixedData.length);
Prints:
6
Methods are special predefined functions that a data structure can call.
Ex-
reverse an array- which reverses the order of elements in an array.
sort- sorts the elements in an array.
push and pop - add and remove elements from an array.
Array.length
var donuts = ["glazed", "powdered", "sprinkled"];
console.log(donuts.length);
Push
push() method to add elements to the end of an array.Also, the push() method returns the length of the array after an element has been added.
var donuts = ["glazed", "chocolate frosted", "Boston creme", "glazed cruller", "cinnamon sugar", "sprinkled"];
donuts.push("powdered"); // the `push()` method returns 7 because the `donuts` array now has 7 elements
Returns: 7
Pop
pop() method to remove elements from the end of an array. Also, pop() returns the element that has been removed in case you need to use it.
var donuts = ["glazed", "chocolate frosted", "Boston creme", "glazed cruller", "cinnamon sugar", "sprinkled", "powdered"];
donuts.pop(); // the `pop()` method returns "powdered" because "powdered" was the last element on the end of `donuts` array
Returns: "powdered"
Splice
splice() is another handy method that allows you to add and remove elements from anywhere within an array. Its starts from 1
var donuts = ["glazed", "chocolate frosted", "Boston creme", "glazed cruller"];
donuts.splice(1, 1, "chocolate cruller", "creme de leche"); // removes "chocolate frosted" at index 1 and adds "chocolate cruller" and "creme de leche" starting at index 1
Returns: ["chocolate frosted"]
donuts array after calling the splice() method: ["glazed", "chocolate cruller", "creme de leche", "Boston creme", "glazed cruller"]
Following is the syntax of splice() method: arrayName.splice(arg1, arg2, item1, ....., itemX); where, arg1 = Mandatory argument. Specifies the starting index position to add/remove items. You can use a negative value to specify the position from the end of the array e.g., -1 specifies the last element. arg2 = Optional argument. Specifies the count of elements to be removed. If set to 0, no items will be removed. item1, ....., itemX are the items to be added at index position arg1 splice() method returns the item(s) that were removed. splice() is an incredibly powerful method that allows you to manipulate your arrays in a variety of ways. Any combination of adding or removing elements from an array can all be done in one simple line of code.
var donuts = ["cookies", "cinnamon sugar", "creme de leche"];
donuts.splice(-2, 0, "chocolate frosted", "glazed");
Prints:
["cookies", "chocolate frosted", "glazed", "cinnamon sugar", "creme de leche"]
The sort() method can sort an array.