You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+131
Original file line number
Diff line number
Diff line change
@@ -296,6 +296,137 @@ The main, difference between the `filter()` and `find`() methods is the return d
296
296
* Filter will always return an array, even if there is only one element or no element in the array.
297
297
* Find will return the element, if there is only one element in the array, otherwise it will return undefined.
298
298
299
+
To apply an operation on the array, we can use the `map()` method. It will return a new array, which contains the result of the operation.
300
+
301
+
```javascript
302
+
let arr = [1, 2, 3, 4, 5];
303
+
304
+
let mapped =arr.map((element) => {
305
+
return element *2;
306
+
});
307
+
console.log(mapped); // prints [2, 4, 6, 8, 10] to the console
308
+
//doubles each and every element of the array
309
+
```
310
+
311
+
To check the presence of an element in the array, we use the `includes()` method. It will return a boolean value, based on the presence of the element.
312
+
313
+
```javascript
314
+
let arr = ["Anurag", "Peddi", "Anuhya", "Chinnu"];
315
+
316
+
console.log(arr.includes("Anurag")); // prints true to the console
317
+
console.log(arr.includes("Anu")); // prints false to the console
318
+
```
319
+
320
+
To merge, all the elements of the array, with a comma separating them, we can use the `.toString()` method.
321
+
322
+
```javascript
323
+
let arr = ["Anurag", "Peddi", "Anuhya", "Chinnu"];
324
+
325
+
console.log(arr.toString()); // prints Anurag,Peddi,Anuhya,Chinnu to the console
326
+
```
327
+
328
+
If you have a different delimiter other than comma, then you can use the `.join()` method.
329
+
330
+
```javascript
331
+
let arr = ["Anurag", "Peddi", "Anuhya", "Chinnu"];
332
+
333
+
console.log(arr.join("$")); // prints Anurag$Peddi$Anuhya$Chinnu to the console
334
+
```
335
+
336
+
## Objects
337
+
338
+
The JavaScript also has the properties of the object-oriented programming language. The objects are defined using the curly braces.
339
+
340
+
```javascript
341
+
let obj = {} // empty object
342
+
let person = {
343
+
firstName:"Anurag",
344
+
lastName:"Peddi"
345
+
}
346
+
```
347
+
348
+
You can access the members of the object using the dot operator.
349
+
350
+
```javascript
351
+
let person = {
352
+
firstName:"Anurag",
353
+
lastName:"Peddi"
354
+
}
355
+
356
+
console.log(person.firstName); // prints Anurag to the console
357
+
```
358
+
359
+
One another way to access the members of the object is using the square brackets.
360
+
361
+
```javascript
362
+
let person = {
363
+
firstName:"Anurag",
364
+
lastName:"Peddi"
365
+
}
366
+
367
+
console.log(person["firstName"]); // prints Anurag to the console
368
+
```
369
+
370
+
The main difference between the dot operator and the square brackets is that, the dot operator will not work if the property name contains the special characters, whereas the square brackets will work.
371
+
372
+
### Iteration
373
+
374
+
You can iterate over the object using the `for-in` loop.
375
+
376
+
```javascript
377
+
let person = {
378
+
firstName:"Anurag",
379
+
lastName:"Peddi"
380
+
job:"Student",
381
+
age:24,
382
+
country:"India"
383
+
}
384
+
385
+
for (var mem in person) {
386
+
console.log("Value of "+ mem +" is "+ person[mem]);
387
+
}
388
+
389
+
// prints
390
+
// Value of firstName is Anurag
391
+
// Value of lastName is Peddi
392
+
// Value of job is Student
393
+
// Value of age is 24
394
+
// Value of country is India
395
+
```
396
+
397
+
### Conversions
398
+
399
+
The objects can be converted to the string using the `JSON.stringify()` method.
400
+
401
+
```javascript
402
+
let person = {
403
+
firstName:"Anurag",
404
+
lastName:"Peddi"
405
+
job:"Student",
406
+
age:24,
407
+
country:"India"
408
+
}
409
+
410
+
console.log(JSON.stringify(person));
411
+
// prints {"firstName":"Anurag","lastName":"Peddi","job":"Student","age":24,"country":"India"} to the console
412
+
```
413
+
414
+
The keys and values of the object can be converted into the array using the `Object.keys()` and `Object.values()` methods.
415
+
416
+
```javascript
417
+
let person = {
418
+
firstName:"Anurag",
419
+
lastName:"Peddi"
420
+
job:"Student",
421
+
age:24,
422
+
country:"India"
423
+
}
424
+
425
+
console.log(Object.keys(person)); // prints ["firstName", "lastName", "job", "age", "country"] to the console
426
+
console.log(Object.values(person)); // prints ["Anurag", "Peddi", "Student", 24, "India"] to the console
427
+
console.log(Object.entries(person)); // prints [["firstName", "Anurag"], ["lastName", "Peddi"], ["job", "Student"], ["age", 24], ["country", "India"]] to the console
428
+
```
429
+
299
430
## Conditions
300
431
301
432
Conditions are used to check whether a condition is true or false, and based on that we can perform some operations.
0 commit comments