Top 10 Must Know JavaScript Hack

Building LMS Backend
JavaScript provides several built-in object methods that can be used to manipulate objects. For example, the Object.keys() method can be used to return an array of an object's keys, the Object.values() method can be used to return an array of an object's values, and the Object.entries() method can be used to return an array of an object's key-value pairs.
In addition to built-in object methods, developers can define their custom methods for objects. Custom object methods can be added to an object using the dot notation or the bracket notation. They can also be defined using the new ES6 shorthand syntax.
One important thing to keep in mind when working with object methods is the value of the "this" keyword. The "this" keyword refers to the current object instance and can be used to access other properties and methods of the object. However, the value of "this" can change depending on how the method is called. In some cases, developers may need to use the "bind", "call", or "apply" methods to set the value of "this" explicitly.
Overall, JavaScript object methods are a powerful tool for working with objects and provide developers with a way to encapsulate related functionality within an object. By leveraging built-in object methods and creating custom methods, developers can create objects that are easier to work with and maintain.
Below Top 10 Javascript CheatSheets can help you to accelerate your development
1=> Checking if a value exists in Object
var obj = {
"a": "test1",
"b": "test2"
}
let exists = Object.values(obj).includes("test1");
2=> Checking if a property exists in the Object
let employee = {
firstName: 'Peter',
lastName: 'Doe',
employeeId: 1
};
console.log('employeeId' in employee); //true
3=> Object.keys(obj) – returns an array of keys.
const anObj = { 100: "a", 2: "b", 7: "c" };
console.log(Object.keys(anObj)); // ['2', '7', '100']
const arr = ["a", "b", "c"];
console.log(Object.keys(arr)); // ['0', '1', '2']
4=> Object.values(obj) – returns an array of values.
const obj = { foo: "bar", baz: 42 };
console.log(Object.values(obj)); // ['bar', 42]
const array =["alam", "jamal"]
console.log(Object.values(array)); // [ 'alam', 'jamal' ]
5=> Object.entries(obj) – returns an array of [key, value] pairs.
const obj = { foo: "bar", baz: 42 };
console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
const arr= ["foo","bar"];
console.log(Object.entries(arr)); // [ ['0', 'bar'], ['1', 42] ]
6=> Loop Over Array to get Index and Value
const arr = ["alam", "jamal"]
for (const [idx, el] of arr.entries()) {
console.log( idx + ': ' + el );
}
7=> Loop Over Object to get Key and Value
const obj = { a: 5, b: 7, c: 9 };
for (const [key, value] of Object.entries(obj)) {
console.log(`${key}..${value}`); // a ..5, b..7, c..9
}
// Using array methods
Object.entries(obj).forEach(([key, value]) => {
console.log(`${key} ${value}`); // a 5, b 7, c 9
});
8=> For in vs For of Loop
const object = { a: 1, b: 2, c: 3 };
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
for (const property of object) {
console.log(`${property}`);
} // Not Iterable
let list = [4, 5, 6];
for (let i in list) {
console.log(i); // "0", "1", "2",
}
for (let i of list) {
console.log(i); // "4", "5", "6"
}
9=> Change the Value of the Object
Javascript objects are created by reference, we can only mutate the original object. that means we can not directly update the value hence we use. myObject[key] = 10 like this
const myObject = { a: 1, b: 2, c: 3 };
Object.keys(myObject).forEach((item) => {
if(typeof myObject[item] == "number" && myObject[item] >= 2) {
myObject[item] = 10 //conditional changing
}
// myObject[item] = true //for all the items
})
Object.entries(myObject).forEach(([key, value]) =>{
if(typeof value == "number" && value >= 2) {
myObject[key] = 10
}
})
console.log(myObject)
10=> Array Property can be Used for object like below
const myObject = { a: 1, b: 2, c: 3 };
Object.keys(myObject).filter((key) => myObject[key] === "1")
//["a"]



