5 useful JavaScript code snippets

Hey guys some useful JavaScript codes for you that make your coding easy. It is really helpful for those who write all the code by him/her self.

In web development there is many websites like Bootstrap 5 & Tailwind CSS that make work lot easy or else I say you just have to copy paste code and you don't have to write single line of code and JavaScript for web development. For those who write code by themself  here some useful codes that's will help you.


1. FINDING A SPECIFIC OBJECT IN AN ARRAY OF OBJECTS

let customers = [
 { id: 0, name: paul },
  { id: 1, name: 'jeff },
 { id: 2, name: 'mary'}
let customer = customers.find(cust => cust.name ===
console.log(customer);
--> { id: 1, name: 'jeff }

2. LOOPING OVER AN OBJECT'S KEYS AND VALUES 

let myObject = { one: 1, two: 2, three: 3 };
Object.keys(myobject).forEach((key, value) => {
 /.do something
 console.log(key, value);
});

3. FILTERING AN ARRAY OF OBJECTS BASED ON A CONDITION

let data = [
 "files/dirl/file",
 "files/dirl/file2",
 "files/dir2/tile",
 "files/dir2/file2"
1;
let filteredData = data.filter(path =»
path.includes('dir2'));
console.log(filteredData);
  > [ files/dir2/file', files/dir2/file2' ]

4. DESTRUCTING VARIABLE ASSIGNMENT

let profile = ['bob', 34, 'carpenter'];
let [name, age, job] = profile;
console.log(name);
--> 'bob'

 5. ASSIGN KEYS TO AN OBJECT WITH THE SAME NAME

let name = 'bob';
let age = 34;
let job = 'carpenter;
// instead of this
let myobjectl = { name: name, age: age, job: job
};
// do this
let myobject2 = { name, age, job };
console.log(myobject2);
 -> { name: 'bob', age: 34, job: 'carpenter' }


Tell me guys it is helpful to you comment down your opinions.  

 

                                                                          

Post a Comment

0 Comments