Javascript Object Methods

Search for a command to run...

No comments yet. Be the first to comment.
💡 “So if you want to go fast, if you want to get done quickly, if you want your code to be easy to write, make it easy to read.” -By Robert C. Martin JavaScript has progressed a ton in recent years. If you are learning JavaScript in 2022 and you ...

Before beginning our quest to find how not to use Create React App. First, let’s see why we need Create React App in the first place. Why do we need Create React App? 🤔 Create React App is a comfortable environment for learning React and is the best...

Getting Started with Node.js In this blog, we will try to understand the basics of Node.js, how it works, and why to choose Node.js, but before diving into node let's talk a little bit about Java-script. JavaScript is one of the best programming lang...

If you are reading this, you probably know what React.js is and might have already used it Earlier. However, you might be wondering why I am reading about folder structure? Can’t I just stuff all my files in the src folder?? 🤪 Technically you could ...

Let’s first understand 🤔 What is an Array? Quoting from MDN directly. The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name and has members for performing common...

//Sample Object
const user = {
name: 'abc',
email: 'abc@.com',
age: 21,
company:'Floxus'
};
In this object, name, email, age, company are the keys, and abc, abc@.com, 21, Floxus are the values.
If you have an array that is considered to be an object in javascript, you can not loop through the array using map(), forEach(), or a for..of loop.
The various methods that can be used to loop through objects in JavaScript are:
Object.keys methodObject.values methodObject.entries methodBefore ES6, the only way to loop through an object was through using the for...in loop.
Now let's code 😎
Example
const user = {
name: 'abc',
email: 'abc@.com',
age: 21,
company:'Floxus'
};
// convert object to key's array
const keys = Object.keys(user);
// print all keys
console.log(keys); //['name', 'email', 'age', 'company']
// iterate over object
keys.forEach((key, index) => {
console.log(`${key}: ${user[key]}`);
});
// name: 'abc',
// email: 'abc@.com',
// age: 21,
// company:'Floxus'
Example
const user = {
name: 'abc',
email: 'abc@.com',
age: 21,
company:'Floxus'
};
// convert object to values array
const values = Object.values(user);
// print all values
console.log(values); //['abc', 'abc@.com', '21', 'Floxus']
// iterate over an object
Object.values(user).forEach(val => console.log(val));
// abc
// abc@.com
// 21
// Floxus
Example
const user = {
name: 'abc',
email: 'abc@.com',
age: 21,
company:'Floxus'
};
// convert object to array
const entries = Object.entries(user);
// print all entries
console.log(entries);
//[['name', 'abc'], ['email', 'abc@.com'], ['age', 21],['company','Floxus']]
// iterate over object
Object.entries(user).forEach(([key, value]) => {
console.log(`${key}: ${value}`)
});
// name: abc,
// email: abc@.com,
// age: 21,
// company:Floxus