Data Structures
A data structure is a format to organize, manage and store data in a way that allows efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to that data.
Objects
In JavaScript, objects are collections of key-value pairs, where each key is a string (or symbol), and the value can be any valid JavaScript value, such as another object, a number, a string, or even a function. Objects are one of the fundamental data types in JavaScript and are commonly used to store and organize data.
Objects can be created using two primary methods:
- Object Literal Syntax :
let person = { name: "John wick", age: 30, greet: function() { console.log("Hello, " + this.name); }};- Using the new Object() constructor :
let person = new Object();person.name = "Adam John";person.age = 25;person.greet = function() { console.log("Hello, " + this.name);};You can modify existing ones or access properties of an object using Dot notation or using Bracket notation .
let person = { name: "John wick", age: 30, greet: function() { console.log("Hello, " + this.name); }
console.log('name=',person.name) // Dot notationconsole.log('name=',person["name"]) // Bracket notation
person.age = 31;console.log(person.age); // 31Arrays
An array is a collection of items stored at contiguous memory locations.
Each item can be accessed through its index (position) number. Arrays always start at index 0, so in an array of 10 elements we could access the 7th element using the index number 6.
The length property of an array is defined as the number of elements it contains.
In JavaScript we can store values of any type in the same array and the length of it can be dynamic. Any data type can be stored in an array, and that includes arrays too.
let arr = ['a', 'b', 1 , 2]
console.log(arr[2]) // 1console.log(arr.length) // 4JavaScript Array Methods
Array length
Return the length of the given array.
const fruits = ["Banana", "Orange", "Apple", "Mango"];console.log(fruits.length) ; // 4Array toString()
Convert an array to a string:
const fruits = ["Banana", "Orange", 4, "Mango"];let text = fruits.toString();console.log(text) ; // Banana,Orange,4,MangoArray join() Method
The join() method returns an array as a string. The join() method does not change the original array. Any separator can be specified. The default is comma (,).
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text1 = fruits.join();let text2 = fruits.join(" | ");
console.log(text1) ; // Banana,Orange,Apple,Mangoconsole.log(text2) ; // Banana | Orange | Apple | MangoArray.push() & Array.pop() Method
Array push() method is used to add new items to an array and pop() method is used to remove items from an array .
The push() method adds new items to the end of an array. The pop() method removes (pops) the last element of an array.The pop() method returns the removed element.
const nums = [1,2,3,4];nums.push(5);
console.log(nums) // [1,2,3,4,5]const nums = [1,2,3,4];nums.pop();
console.log(nums) // [1,2,3]Array.sort() Method
The sort() method sorts the elements of an array.
const nums = [2,5,3,9];const text = ['f','a','d','s']text.sort();nums.sort();
console.log(text) // [a,d,f,s]console.log(nums) // [2,3,5,9]Array.reduce() Method
The reduce() method returns a single value: the function’s accumulated result.And does not execute the function for empty array elements.
const array1 = [1, 2, 3, 4];
const initialValue = 0;const sumWithInitial = array1.reduce( (accumulator, currentValue) => accumulator + currentValue, initialValue,);
console.log(sumWithInitial); // 10Array.map() Method
The map() method of Array instances creates a new array populated with the results of calling a provided function on every element in the calling array.
const arr = [1, 4, 9, 16];
// Pass a function to mapconst map1 = arr.map((x) => x * 2);
console.log(map1); // [2, 8, 18, 32]Array.reverse() method
The reverse() method reverses the order of the elements in an array.
const fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.reverse();console.log(fruits); // [ "Mango", "Apple", "Orange", "Banana" ]Array.forEach() Method
he forEach() method calls a function for each element in an array.
const array1 = ['a', 'b', 'c'];
array1.forEach((element) => console.log(element));// 'a'// 'b'// 'c'Array.indexOf() Method
The indexOf() method returns the first index (position) of a specified value. And returns -1 if the value is not found.
const array1 = ['a', 'b', 'c'];
console.log(array1.indexOf('b')); // 1
console.log(array1.indexOf('e')); // -1