Javascript : Arrays and Objects
Tags: teamtanayejschallenge, javascript, eloquentjs
Published: 2020-12-07 12:45:49

###Arrays
Arrays are a data type that can store a list of sequences of values. It is written as a list of values between square brackets, separated by commas.
All arrays are 0 indexed in javascript, i.e., the first element starts off with index 0 rather than the conventional 1. This is standard across all coding languages, and has been kind of a tradition.
```javascript
let list = [1, 2, 3, 4, 5];
//You can access each value inside this array as : 
console.log("The first element is : "+list[0]);
//The first element is 1
```
The two main ways to access properties in JavaScript are with a dot and with square brackets. Both value.x and value[x] access a property on value —but not necessarily the same property. The difference is in how x is interpreted.
When using a dot, the word after the dot is the literal name of the property.
When using square brackets, the expression between the brackets is _evaluated_ to __get__ the property name. Whereas __value.x__ fetches the property of value named “x”, __value[x]__ tries to evaluate the expression x and uses the result, converted to a string, as the property name.

####Methods
* Properties that contain functions are generally called _methods_ of the value they belong to, for example, _"toUpperCase"_ is a method of string.
* We can manipulate the arrays in JS using two main methods : ___push___ and ___pop___ .
```javascript
let sequence = [1, 2, 3];
sequence.push(4); //push method adds an element at the end of the array
sequence.push(5);
console.log(sequence);
// → [1, 2, 3, 4, 5]
console.log(sequence.pop()); //pop method removes the last element of the array
// → 5
console.log(sequence);
// → [1, 2, 3, 4]
```
###Objects
Values of the type object are arbitrary collections of properties. We usually define objects using curly braces `{}` . We can access the elements of individual properties within an object using the `.` operator.
```javascript
let day1 = {
squirrel: false,
events: ["work", "touched tree", "pizza", "running"]
};
console.log(day1.squirrel);
// → false
console.log(day1.wolf);
// → undefined
day1.wolf = false;
console.log(day1.wolf);
// → false
```
You can use the **delete** operator to delete a property of an object. For example
```javascript
let anObject = {left: 1, right: 2};
console.log(anObject.left);
// → 1
delete anObject.left;
console.log(anObject.left);
// → undefined
console.log(anObject.right);
// → 2

console.log("left" in anObject);
// → false
console.log("right" in anObject);
// → true
```
The binary __in__ operator can be used to check if an object has a property with the particular name. To find the keys in an object, you can use __Object.Keys__ .
```javascript
console.log(Object.keys({x: 0, y: 0, z: 2}));
// → ["x", "y", "z"]
```
####Array loop
The first loop is below is a normal for loop, going over each element one by one. However, the counter here has no purpose other than referencing each element.
```javascript
for (let i = 0; i < JOURNAL.length; i++) {
let entry = JOURNAL[i];
// Do something with entry
}

for (let entry of JOURNAL) {
console.log(`${entry.events.length} events.`);
}
```
The second loop also iterates through the entire array, and each element is sequentially taken as entry, and used inside the loop. 
Both the loops are used and work well.

####Some more methods
* Just like __push__ and __pop__ work at the last index of an array, __shift__ and __unshift__ are used to add and remove elements at the start of an array.
```javascript
let todoList = [];
function remember(task) {
todoList.push(task);
}
function getTask() {
return todoList.shift();
}
function rememberUrgently(task) {
todoList.unshift(task);
}
```
* To search for a specific value, there is an __indexOf__ method. This returns the first index from the start. If you wish to start searching from the last index, you have __lastIndexOf__ .
```javascript
console.log([1, 2, 3, 2, 1].indexOf(2));
// → 1
console.log([1, 2, 3, 2, 1].lastIndexOf(2));
// → 3
//Both these methods take in an optional second argument,
//that indicates from where to start searching
```
* We also have the __slice__ and __concat__ methods to perform slicing and concatenation respectively.
```javascript
function remove(array, index) {
return array.slice(0, index)
.concat(array.slice(index + 1));
}
console.log(remove(["a", "b", "c", "d", "e"], 2));
// → ["a", "b", "d", "e"]
```
###Strings and their methods
* One of the most useful ones are __slice__ and __indexOf__ .
```javascript
console.log("coconuts".slice(4, 7));
// → nut
console.log("coconut".indexOf("u"));
// → 5
```
* The __trim__ method removes whitespaces, and other extra symbols from a given string.
```javascript
console.log("Hey param \n ".trim());
// → Heyparam
```
* The __padStart__ method allows us to pad a given string, taking in the length and the padding character as arguments. 
```javascript
console.log(String(6).padStart(3, "0"));
// → 006
```
* You can use the __split__ and __join__ methods on strings.
```javascript
let sentence = "Secretarybirds specialize in stomping";
let words = sentence.split(" ");
console.log(words);
// → ["Secretarybirds", "specialize", "in", "stomping"]
console.log(words.join(". "));
// → Secretarybirds. specialize. in. stomping
```
* You can use the __repeat__ method to create multiple copies of a string
```javascript
console.log("LA".repeat(3));
// → LALALA
```

####Rest Parameters
Sometimes, we may want to have a function to have any number of arguments. This can be done by adding in 3 dots `...` before the last parameter of the function.
```javascript
function max(...numbers) {
let result = -Infinity;
for (let number of numbers) {
if (number > result) result = number;
}
return result;
}
console.log(max(4, 1, 9, -2));
// → 9
```
###The JSON Object
We often need to store information in objects, or use it for later use. To do this effectively, we _serialize_ the data. This means that it will be stored in a flat description. One of the most popular ways is to store it in __JSON (JavaScript Object Notation)__ format. This is used not only for JS, but also used across the web to send and store information.
```javascript
{
"squirrel": false,
"events": ["work", "touched tree", "pizza", "running"]
}
```