Our first stop in our blog series on “Understanding Data Structure and algorithm with JavaScript 🚀” is Array, String, and Object manipulation. To grasp the good hold in Data structure using JavaScript, these are the major three topics that serve as the most important basic components which build the base in learning and understanding any advanced data structure.
What is Data Structure?
A Data structure is a specialized way of organizing, processing, and storing the given data in the memory so that at any given point, It can be used efficiently. There are many ways to store data in the memory like an array, string, or object. There are basically two types of data structure:-
Primitive Data Structure
Primitive Data structure are basically data types like int, chart, float or pointer mostly used in Statically–typed language like C, C++ or Java.
Non-Primitive Data Structure
Non-Primitive Data structures are data structure which is created using Primitive data structures. There are two types of Non-Primitive Data Structures:-
Linear data structure
Linear Data structure is a method of organizing data in a linear sequential form. Like Stack, Queue, Linked list, and Arrays. In these data structures each element is connect in a sequential manner so that it makes it easier to arrange and implement because the computer’s memory is also arranged in linear form and traverses all elements in a single run.
Non-Linear data structure
Non-Linear Data structure is the data structures that are not organized in linear forms, like Graphs and Trees. Non-Linear Data structures cannot be easily implemented and It is not possible to traverse all the elements in a single run, But in Non-Linear Data Structure memory is utilized in a more efficient way than Linear data structures.
Also read, An Easy Explanation of JavaScript Closure
Major Operations Performed
The most commonly operations performed on Data structures are:-
- Sorting – Arranging the elements of the data structure in ascending or descending order.
- Searching – Searching/finding an element or the location of the particular element in the data structure.
- Insertion – Adding any element either at starting of the data structure, in the end, or at the middle of the data structure.
- Updation – Modifying or updating the data structure.
- Deletion – Removing or deleting an element or set of elements from the data structure.
- Traversal – Traversal is a method to reach every element or node in a particular manner.
Advantages of Data Structure
- Efficiency – Data Structure is the best way to organize the given data in such a way that it can efficiently use the memory space and is very easy to retrieve, update, search or delete any element.
- Abstraction – A lot of Data structure is kind of made of other types of data structure like Stack or Queue is based on arrays or Trees which is based on linked list.
- Reusability – Data structure can be used multiple times in the program or multiple programs can use a data structure.
Disadvantages of Data Structure
- Creating or designing your own data structure can be very tough, one has to put a lot of time and test with numerous types of edge cases before concluding and start using for large scale organizational purpose.
- Implementing and learning these data structure operations, ways to implement, and understanding efficiency requires a lot of practice.
- In a bigger application, when implementing the data structure one has to know which type of data structure will be suitable for the data you are trying to process, and maintaining the application requires more professional resources.
Array
An Array is a collection of elements where each value is referenced using an index or key, stored in the contiguous memory location. You can assume them as a train, where each cart is named with the index starting from ‘0’ and it contains elements, or an apartment, where each floor has elements and the ground floor is termed from ‘0’. But it can be a little different from language to language as in swift, java, or c++, the elements can only have homogenous or single data types or in Javascript or Python, elements can be heterogeneous or have multiple data types, but fundamental concepts remain the same.
In JavaScript, Array is a class global object with array characteristics, which is used to create high list-level objects. It is important to remember that JavaScript Arrays, doesn’t have any fixed length nor the data types of each element should be similar. And since JavaScript Arrays don’t have a fixed length, so data can be stored at a non-contiguous memory location.
Though it is advisable to have all elements in an array of single data types to avoid any error. Now let us understand some basic operations with Array in JavaScript,
Creating a Array
An Array can be initialized in many ways,
// Initializing the empty array
let arr = []
or
// Initializing using constructor
let arr = new Array()
// Initializing the array with values
let arr = [1,2,3,4,5,6]
let arr = new Array(1,2,3,4,5,6)
Traversing single or all Elements of Array
We can easily reach or console every element of the array, either using a particular index or using for loop and you also use the length property to print the length of the array.
let arr = [1,2,4,7,5,3,6,8,9]
//you can print the length
console.log('Length of the Array is',arr.length)
//Length of the Array is 9
//To print the first element or element on index 5 Big-O - O(1)
console.log(arr[0])
console.log(arr[5])
//1
//3
//To print all the numbers of the array Big-O - O(n)
for(let i = 0; i < arr.length; i++){
console.log(arr[i])
}
//1
//2
//4
//7
//5
//3
//6
//8
Also read, What is a Callback Functions in JavaScript
List of Some JavaScript Array Methods
JavaScript arrays come with a lot of pre-build powerful functions where each function performs various calculations saving us to write everything from scratch, here are some popular array methods
1. map()
map() method is the most useful and widely used array method, it is used to return a new array, after executing provided function to each element of the given array. It does not affect the original array and returns the new array of the same length as of original array.
Time Complexity – O(n)
let arr = [1,2,3,4,5]
let newArr = arr.map(item => item * 2)
console.log(arr) // [1, 2, 3, 4, 5]
console.log(newArr) // [2, 4, 6, 8, 10]
let months = ['jan','feb','march','april']
let newMonths = months.map(data => data.toUpperCase())
console.log(months) // ['jan','feb','march','april']
console.log(newMonths) // ["JAN", "FEB", "MARCH", "APRIL"]
2. filter()
filter() method is used to return a new array, after the elements which satisfy the given condition in the provided function. It also does not affect the original array and returns the new array of the same length as of original array.
Time Complexity – O(n)
let arr = [1,2,3,4,5]
let newArr = arr.filter(item => item > 2)
console.log(arr) // [1, 2, 3, 4, 5]
console.log(newArr) // [3, 4, 5]
let arr1 = [1,2,3,4,5,6,7,8,9,10]
let newArr1 = arr1.filter(item => item % 2 == 0)
console.log(arr1) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(newArr1) // [2, 4, 6, 8, 10]
3. forEach()
forEach() method is also one of the most useful methods, it is used to execute provided function to each element of the given array. It does not affect the original array and does not return the new array, but even if you explicitly return anything or save it to a variable, then the return value would be undefined.
Time Complexity – O(n)
let arr = [1,2,3]
let result = arr.forEach(item => console.log(item * 2))
// 2
// 4
// 6
console.log(result) // undefined
4. sort()
sort() method is used to arrange the elements of the given array either in ascending or descending order, by default it arranges the elements in the ascending order. It converts the element into strings, then compares their sequences according to the UTF-16 code unit values.
It affects the original array and also returns the new array of the same length as of original array.
Time Complexity – O(n * log n)
let arr = [4, 2, 5, 1, 3]
let result = arr.sort()
console.log('Orginal array',arr) // "Orginal array", [1, 2, 3, 4, 5]
console.log('New return array',result) // "New return array", [1, 2, 3, 4, 5]
// you can also provide function expressions to determine the order
let arr1 = [6, -3, -10, 0, 2, 8]
// to sort in ascending order
arr1.sort((a,b) => a - b)
console.log(arr1) // [-10, -3, 0, 2, 6, 8]
// to sort in descending order
arr1.sort((a,b) => b - a)
console.log(arr1) // [8, 6, 2, 0, -3, -10]
5. concat()
concat() method is used to merge two or more arrays and return a new array. It does not affect the original arrays and returns the new array of the sum of all the array lengths.
Time Complexity – O(n)
let arr = [1,2,3,4,5]
let arr1 = [6,7,8,9,10]
let arr2 = [11,12,13,14,15]
// merging two arrays
let result1 = arr.concat(arr1)
console.log(arr) // [1, 2, 3, 4, 5]
console.log(arr1) // [6, 7, 8, 9, 10]
console.log(result1) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
//merging two and more arrays
let result2 = arr.concat(arr1,arr2)
console.log(arr) // [1, 2, 3, 4, 5]
console.log(arr1) // [6, 7, 8, 9, 10]
console.log(arr2) // [11, 12, 13, 14, 15]
console.log(result2) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
6. some()
some() method is used to return a true boolean value if at least one element of the given array is able to pass the provided condition, else if not then returns false boolean value and It does not affect the original array.
Time Complexity – O(n)
let arr = [2,5,7,4,6,1]
let result = arr.some(item => item > 8)
let result2 = arr.some(item => item % 3 == 0)
console.log(arr) // [2, 5, 7, 4, 6, 1]
console.log(result) // false
console.log(result2) // true
7. includes()
includes() method is used to determine that a certain value is present among the elements in the given array or not and returns true or false accordingly. It also does not affect the original array.
Time Complexity – O(n)
let arr = [2,5,7,4,6,1]
let result = arr.includes(4)
let result2 = arr.includes(9)
console.log(arr) // [2, 5, 7, 4, 6, 1]
console.log(result) // true
console.log(result2) // false
8. join()
join() method is used to return a new string, after concatenating all the elements of the given array separated with a specified separator and It does not affect the original array.
Time Complexity – O(n)
//
let arr = ['A','p','p','l','e']
let result = arr.join() // giving no seprator
let result1 = arr.join('') // giving empty seprator
console.log(arr) // ["A", "p", "p", "l", "e"]
console.log(result) // "A,p,p,l,e"
console.log(result1) // "Apple"
let arr1 = ['This','is','so','awesome']
let result2 = arr1.join(' ') // giving space as a seprator
let result3 = arr1.join('-') // giving - as a seprator
console.log(arr1) // ["This", "is", "so", "awesome"]
console.log(result2) // "This is so awesome"
console.log(result3) // "This-is-so-awesome"
let arr2 = [2,5,1,6,9]
let result4 = arr2.join('--') // giving -- as a seprator
console.log(arr2) // [2, 5, 1, 6, 9]
console.log(result4) // "2--5--1--6--9"
9. reverse()
reverse() method is used to reverse the order of the element of the given array. It affects the original array but returns the new array of the same length as of original array.
Time Complexity – O(n)
//
let arr = ['A','p','p','l','e']
let result = arr.reverse()
console.log(arr) // ["e", "l", "p", "p", "A"]
console.log(result) // ["e", "l", "p", "p", "A"]
let arr1 = [2,4,7,1,9]
let result2 = arr1.reverse()
console.log(arr1) // [9, 1, 7, 4, 2]
console.log(result2) // [9, 1, 7, 4, 2]
10. every()
every() method is used to check if each element present in the given array satisfies the given condition or not and returns true and false accordingly and It also does not affect the original array.
Time Complexity – O(n)
//
let arr = [1,2,3,4,5]
let result = arr.every(item => item < 9)
console.log(arr) // [1, 2, 3, 4, 5]
console.log(result) // true
let arr1 = [2,4,7,1,9]
let result2 = arr1.every(item => item % 2 == 0)
console.log(arr1) // [2, 4, 7, 1, 9]
console.log(result2) // false
11. reduce()
reduce() method is also one of the most useful array methods, it act as a reducer and executes reducer function to each element of the array and is used to return a single value. It also does not affect the original array.
Time Complexity – O(n)
// reduce((accumulator, currentValue) => { ... } )
const arr = [1, 2, 3, 4];
const reducer = arr.reduce((accumulator, currentValue) => accumulator + currentValue)
console.log(arr) // [1, 2, 3, 4]
console.log(reducer) // 10
// reduce((accumulator, currentValue, index, array) => { ... }, initialValue)
const arr1 = [2,4,8,9,4];
const reducer1 = arr1.reduce((acc, curr) => {
return acc + curr
},3)
console.log(reducer1)
12. find()
find() method is used to return a very first element from the given array which passes the given condition and It also does not affect the original array.
Time Complexity – O(n)
const arr = [1, 2, 3, 4];
const result = arr.find(item => item > 2)
console.log(arr) // [1, 2, 3, 4]
console.log(result) // 3
13. findIndex()
findIndex() method is used to return an index of the very first element from the given array which passes the given condition and It also does not affect the original array.
Time Complexity – O(n)
const arr = [1, 2, 3, 4];
const result = arr.findIndex(item => item > 2)
console.log(arr) // [1, 2, 3, 4]
console.log(result) // 2
14. indexOf()
indexOf() method is used to return the index of the very first element of the given array which matches the value given by the user and if not then it returns -1. It also does not affect the original array.
Time Complexity – O(n)
const arr = [1, 2, 3, 4];
const result = arr.indexOf(2)
console.log(arr) // [1, 2, 3, 4]
console.log(result) // 1
const arr1 = ["Apple","Mango", "WaterMelon", "Grapes"]
const result1 = arr1.indexOf('Oranges')
console.log(arr1) // ["Apple", "Mango", "WaterMelon", "Grapes"]
console.log(result1) // -1
15. fill()
fill() method is used to fills the given array with given value and return the modified array. It does affect the original array.
Time Complexity – O(n)
const arr = new Array(3);
const result = arr.fill(2)
console.log(arr) // [2, 2, 2]
console.log(result) // [2, 2, 2]
16. slice()
slice() method is used to return a new array by removing the elements falling out of specific indexes, starting-index, and ending-index and It does not affect the original array. It is important to note that, it includes all the elements from the given starting index and removes all the elements including the element from the ending index.
Time Complexity – O(n)
//arr.slice(starting index, ending index)
const arr = [1,2,3,4,5,6]
const result = arr.slice(1,4)
console.log(arr) // [1, 2, 3, 4, 5, 6]
console.log(result) // [2, 3, 4]
17. push()
push() method is used to add a new element at the end of the given array and returns a new array. It affects the original array and returns the new length of the array.
Time Complexity – O(1)
const arr = [1,2,3,4,5,6]
const result = arr.push(7)
console.log(arr) // [1, 2, 3, 4, 5, 6, 7]
console.log(result) // 7
18. pop()
pop() method is used to remove an element which is present at the end of the given array. It affects the original array and returns that element.
Time Complexity – O(1)
const arr = [1,2,3,4,5,6]
const result = arr.pop()
console.log(arr) // [1, 2, 3, 4, 5]
console.log(result) // 6
19. shift()
shift() method is used to remove an element which is present at the starting of the given array. It affects the original array and returns that element.
Time Complexity – O(n)
const arr = [1,2,3,4,5,6]
const result = arr.shift()
console.log(arr) // [2, 3, 4, 5, 6]
console.log(result) // 1
20. unshift()
unshift() method is used to add one or two elements at the starting position or index of the given array. It affects the original array and returns the new length of the array.
Time Complexity – O(n)
const arr = [1,2,3,4,5,6]
const result = arr.unshift(0)
console.log(arr) // [0, 1, 2, 3, 4, 5, 6]
console.log(result) // 7
Also read, Where Non-Techies Can Get With the Programming
Strings
Strings data structures can be defined as an array of characters. It is very useful to hold data that can be represented in text form. In JavaScript, Strings are objects which are used to represent and manipulate the sequence of characters.
Creating a String
There is numerous way to initializing a string, It’s important to remember whenever you are initializing a string don’t forget to put it between double or single quotes and if you don’t put it inside any quotes it will give Uncaught SyntaxError: Unexpected identifier error.
// Initializing empty string
let st ="";
// checking the data type of st variable
console.log(typeof(st)) // "string"
// Initializing with some values
let str = 'This is string'
// Initializing using constructor
let str1 = new String("This is another string");
Traversing single or all Elements of a String
There are lot of different ways we can console any character from the string, either using the index or for loop,
let str = 'This is string'
//To print the first element or element on index 8
// Time Complexity : Big-O - O(1)
console.log(str[0]) // T
console.log(str[8]) // S
// To print all the numbers of the array
// Time Complexity : Big-O - O(n)
let sts = 'Apple'
for(let st in sts){
console.log(sts[st])
}
// A
// p
// p
// l
// e
List of Some JavaScript String Methods
In JavaScript, String object also comes with lot of different pre-build methods and properties performing various functions,
1. length()
length() method is used to return the length of the given string.
Time Complexity – O(n)
let str = 'This is string methods'
console.log(str.length) // 22
2. toLowerCase
toLowerCase() method is used to convert all of the characters in the given string to lowercase. It does not affect the original string.
Time Complexity – O(n)
let str = 'This is String'
let sts = str.toLowerCase()
console.log(str) // "This is String"
console.log(sts) // "this is string"
3. toUpperCase()
toUpperCase() method is used to convert all of the characters in the given string to upper case. It does not affect the original string.
Time Complexity – O(n)
let str = 'This is String'
let sts = str.toUpperCase()
console.log(str) // "This is String"
console.log(sts) // "THIS IS STRING"
4. chartAt()
charAt() method is used to return a character present at the given index, inside the given string. It does not affect the original string.
Time Complexity – O(n)
let str = 'This is String'
let sts = str.charAt(5)
console.log(str) // "This is String"
console.log(sts) // "i"
5. charCodeAt()
charCodeAt() method is used to return a Unicode value of the character present at the given index, inside the given string. It does not affect the original string.
Time Complexity – O(n)
let str = 'This is String'
let sts = str.charCodeAt(5)
console.log(str) // "This is String"
console.log(sts) // 105
6. replace()
replace() method is used to return a new string, after replacing some or all matching patterns with the given replacement. The pattern can be a string or a regular expression and the given replacement can also be a string or replacer function, the replacer function is called to create a new substring which is used to replace the matches of the given string or regexp and It does not affect the original string.
Time Complexity – O(n)
let str = 'Apple is a fruit'
let sts = str.replace('Apple','Orange')
console.log(str) // "Apple is a fruit""
console.log(sts) // "Orange is a fruit"
let sts1 = str.replace('Banana','Orange')
console.log(str) // "Apple is a fruit"
console.log(sts1) // "Apple is a fruit"
let sts2 = str.replace(/is/i, 'are')
console.log(str) // "Apple is a fruit"
console.log(sts2) // "Apple are a fruit"
str.replace(/fruit/i, () => console.log('found')) // found
7. includes()
includes() method is used to return true or false if the given substring is present in the string or not. A position parameter can also be passed, which determines the program from which positions it can start searching, by default it starts the search from the index 0 and It does not affect the original string.
Time Complexity – O(n)
let str = 'Apple is a fruit'
let sts = str.includes('Apple')
let stp = str.includes('Banana')
console.log(str) // "Apple is a fruit""
console.log(sts) // true
console.log(stp) // false
let st = 'The Japanese yen for commerce is still well-known'
let result = st.includes('yen', 4)
let result2 = st.includes('yen', 15)
console.log(result) // true
console.log(result2) // false
8. match()
match() method is used to find the match in the string against the given regular expression and returns a new array. It does not affect the original string.
Time Complexity – O(n)
let str = 'Apple is Fruit'
// return the character which is in uppercase
let result = str.match(/[A-Z]/g)
console.log(str) // "Apple is a fruit""
console.log(result) // ["A", "F"]
9. substring()
substring() method is used to return a string after removing all the element, which falls out of the given starting and ending index, it is important to remember that element from starting index is included and element at ending index is excluded. It does not affect the original string.
Time Complexity – O(n)
let str = 'Apple is Fruit'
let result = str.substring(0,3)
console.log(str) // "Apple is a fruit""
console.log(result) // "App"
10. endsWith()
endsWith() method is used to return true or false if the strings end with a specific string or characters and it does not affect the original string.
Time Complexity – O(n)
let str = 'Apple is fruit'
let result = str.endsWith('fruit')
let result1 = str.endsWith('vegetable')
console.log(str) // "Apple is a fruit""
console.log(result) // true
console.log(result1) // false
11. repeat()
repeat() method is used to return a new string witch containes specified number of copies of the given string and It does not affect the original string.
Time Complexity – O(n)
let str = 'Apple'
let result = str.repeat(2)
console.log(str) // "Apple"
console.log(result) // "AppleApple"
12. trim()
trim() method is used to remove whitespace from both ends of the given string and It does not affect the original string.
Time Complexity – O(n)
let str = ' Apple is a fruit '
let result = str.trim()
console.log(str) // " Apple is a fruit "
console.log(result) // "Apple is a fruit"
13. search()
search() method is used to find the pattern in the string against the provided regular expression, if found it returns the index, and if not the returns -1. It does not affect the original string.
Time Complexity – O(n)
let str = 'There are 100 of apples!'
// any character that is not a word character or whitespace
let result = str.search(/[^\w\s]/g)
console.log(str) // "There are 100 of apples!"
console.log(result) // 23
14. indexOf()
indexOf() method is used to search the given string or characters and returns the index and -1 if not found. You can also give the starting position from which you can start searching from and It does not affect the original string.
Time Complexity – O(n)
let str = 'There are the 100 of apples in the basket'
let result = str.indexOf('apples')
let result1= str.indexOf('z')
console.log(str) // "There are 100 of apples!"
console.log(result) // 21
console.log(result1) // -1
let result2 = str.indexOf('the')
let result3 = str.indexOf('the',20)
console.log(result2) // 10
console.log(result3) // 31
15. split()
split() method is the most useful and widely used string method, It is used to return a new array, in which strings are seprated or split by given seprator, the seprator can be both a simple string or a regular expression.
We can also pass a limiter, which is a non-negative integer that specifies a maximum number of elements that can be in a string and does not affect the original string.
Time Complexity – O(n)
let str = 'Apple is a fruit'
let result = str.split()
let result1 = str.split(' ')
let result2 = str.split(' ', 3)
console.log(str) // "Apple is a fruit""
console.log(result) // ["Apple is a fruit"]
console.log(result1) // ["Apple", "is", "a", "fruit"]
console.log(result2) // ["Apple", "is", "a"]
Also read, How does JavaScript works ? A detail explanation of JavaScript’s Engine Working
Objects
JavaScript language is based on an object model, basically, everything in JavaScript is an object which is not primitive. In JavaScript, an object is an unordered collection of key-value pairs, known as properties. Each key is known as a property name that has a unique value and can be a string that holds a value, known as the property value.
Property value can be any valid JavaScript value, like a string, a number, an array and even a function.
Creating a String
There are lot of different ways to create an object,
// initialize empty object
let obj = {}
// Initializing using constructor
let obj1 = new Object();
// static method
const obj2 = Object.create({ })
// Initializing the obj with values
let obj3 = {
firstName:'Aaquib',
lastName:'Ahmed',
age:23,
profession:'Developer'
}
Accessing Key and Value of an Object
In order to access any property of an object you can use the dot notations or an array like notations.
let User = {
firstName:'Aaquib',
lastName:'Ahmed',
age:23,
profession:'Developer'
}
// to access first name using dot notations
console.log(User.firstName) // "Aaquib"
// to access first name using array-like notations
console.log(User['firstName']) // "Aaquib"
You can even have space in the property name but remember, to put it in the single or double quotes. Note – If you are using space in the property name, then you only access the property using array-like notations, if not then will throw an error, Uncaught SyntaxError: Unexpected string. And if you are accessing any property name which is not present inside the object then, it will output undefined.
For best practices, always use single string as a property name.
let User = {
firstName:'Aaquib',
lastName:'Ahmed',
age:23,
profession:'Developer',
'Phone no':317642897
}
// to access first name using dot notations
console.log(User.'Phone no')
// Uncaught SyntaxError: Unexpected string
// to access first name using array-like notations
console.log(User['Phone no']) // 317642897
console.log(User.address) // undefined
Time Complexity – O(1)
Iterating over properties of an object using for...in
loop
You can also iterate through all the properties of an object without knowing properties name, using for…in loop method,
Time Complexity – O(n)
let User = {
firstName:'Aaquib',
lastName:'Ahmed',
age:23,
profession:'Developer',
'Phone no':317642897
}
for(let key in User){
console.log(key)
}
// "firstName"
// "lastName"
// "age"
// "profession"
// "Phone no"
Adding a new property to an object
You can also add a new property to an object easily,
Time Complexity – O(1)
let User = {
firstName:'Aaquib',
lastName:'Ahmed',
age:23,
profession:'Developer',
'Phone no':317642897
}
// adding new property
User.address = 'Bangalore'
console.log(User)
//{
// address: "Bangalore",
// age: 23,
// firstName: "Aaquib",
// lastName: "Ahmed",
// Phone no: 317642897,
// profession: "Developer"
//}
List of Some JavaScript Objects Methods
1. Delete
You can also easily delete any property from an object, to delete a property from an object we have to use the delete operator.
Time Complexity – O(1)
let User = {
firstName:'Aaquib',
lastName:'Ahmed',
age:23,
profession:'Developer',
'Phone no':317642897
}
// deleteing 'Phone no' property
delete User['Phone no']
console.log(User)
//{
// age: 23,
// firstName: "Aaquib",
// lastName: "Ahmed",
// profession: "Developer"
//}
2. Object.keys()
Object.keys() method is used to return a array containing all the property name.
Time Complexity – O(n)
let User = {
firstName:'Aaquib',
lastName:'Ahmed',
age:23,
profession:'Developer',
}
console.log(Object.keys(User))
//["firstName", "lastName", "age", "profession"]
3. Object.values()
Object.values() method is used to return a array containing all the property value.
Time Complexity – O(n)
let User = {
firstName:'Aaquib',
lastName:'Ahmed',
age:23,
profession:'Developer',
}
console.log(Object.values(User))
//["Aaquib", "Ahmed", 23, "Developer"]
3. Object.entries()
Object.entries() method is used to return an array containing all the enumerable string-keyed property in [key, value]
pairs.
Time Complexity – O(n)
let User = {
firstName:'Aaquib',
lastName:'Ahmed',
age:23,
profession:'Developer',
}
console.log(Object.entries(User))
//[["firstName", "Aaquib"], ["lastName", "Ahmed"], ["age", 23], ["profession", "Developer"]]
3. Object.freeze()
Object.freeze() method is used to freeze the object, which means you can’t update or delete any property.
Time Complexity – O(n)
let User = {
firstName:'Aaquib',
lastName:'Ahmed',
age:23,
profession:'Developer',
}
Object.freeze(User)
User.age = 24
// Throws an error in strict mode
Also read, What is Big O Notation – Space and Time Complexity
Final Words
I hope you liked and enjoyed this article, and if you find this article please share it more with your friends and colleagues. In the next blog series of “Understanding Data Structure and algorithm with JavaScript 🚀”, I would be discussing searching algorithms, their importance, and algorithms implementation.
Please bookmark this website to get awesome articles like these and do check our detailed researched articles on various topics like business, technology, and software.