Skip to content

Commit f58ecd3

Browse files
committed
added examples to object section
1 parent 920afc2 commit f58ecd3

File tree

1 file changed

+116
-26
lines changed

1 file changed

+116
-26
lines changed

JavaScript-Quick-Reference.md

Lines changed: 116 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,13 @@ switch (expression) {
381381
Detailed guide on JavaScript Arrays, covering array manipulation methods, iteration, and array-specific operations.
382382

383383
### Properties
384-
- `Array.length` Reflects the number of elements in an array.
385-
- `Array.prototype` Represents the prototype for the Array constructor and allows to add new properties and methods to all Array objects.
384+
- `array.length` Reflects the number of elements in an array.
385+
- `array.prototype` Represents the prototype for the Array constructor and allows to add new properties and methods to all Array objects.
386386

387387
### Methods
388-
- `Array.from(arrayLike[, mapFn[, thisArg]])` Creates a new Array instance from an array-like or iterable object.
389-
- `Array.isArray(obj)` Returns true if a variable is an array, if not false.
390-
- `Array.of("element1", "element2");` Creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.
388+
- `array.from(arrayLike[, mapFn[, thisArg]])` Creates a new Array instance from an array-like or iterable object.
389+
- `array.isArray(object)` Returns true if a variable is an array, if not false.
390+
- `array.of("element1", "element2");` Creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.
391391

392392
### Mutator Methods
393393
- `arr.copyWithin(target, start, end)` Copies a sequence of array elements within the array.
@@ -398,34 +398,95 @@ Detailed guide on JavaScript Arrays, covering array manipulation methods, iterat
398398
- `arr.reverse()` Reverses the order of the elements of an array in place — the first becomes the last, and the last becomes the first.
399399
- `arr.shift()` Removes the first element from an array and returns that element.
400400
- `arr.sort()` Sorts the elements of an array in place and returns the array.
401-
- `array.splice(start, deleteCount, item1, item2)` Adds and/or removes elements from an array.
401+
- `array.splice(start, deleteCount, element1, element2)` Adds and/or removes elements from an array.
402402
- `arr.unShift("element1", "element2");` Adds one or more elements to the front of an array and returns the new length of the array.
403403

404+
### Example of arrray.sort()
405+
406+
```javascript
407+
// Assume we have an array of objects with a 'category' property
408+
const items = [
409+
{ name: 'Banana', category: 'Fruit' },
410+
{ name: 'Carrot', category: 'Vegetable' },
411+
{ name: 'Apple', category: 'Fruit' },
412+
{ name: 'Lettuce', category: 'Vegetable' },
413+
{ name: 'Orange', category: 'Fruit' }
414+
];
415+
416+
// Sort the items by category in ascending order
417+
items.sort((a, b) => {
418+
if (a.category < b.category) {
419+
return -1;
420+
}
421+
if (a.category > b.category) {
422+
return 1;
423+
}
424+
return 0;
425+
});
426+
427+
console.log(items);
428+
429+
// Outputs:
430+
[
431+
{ name: 'Banana', category: 'Fruit' },
432+
{ name: 'Apple', category: 'Fruit' },
433+
{ name: 'Orange', category: 'Fruit' },
434+
{ name: 'Carrot', category: 'Vegetable' },
435+
{ name: 'Lettuce', category: 'Vegetable' }
436+
]
437+
```
438+
404439
### Acessor Methods
405-
- `arr.at(index)` Returns the element at the specified index in the array.
406-
- `arr.concat(value1, value2, array2)` Returns a new array comprised of this array joined with other array(s) and/or value(s).
407-
- `arr.includes(searchElement, fromIndex)` Determines whether an array contains a certain element, returning true or false as appropriate.
408-
- `arr.indexOf(searchElement[, fromIndex])` Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
409-
- `arr.join(separator)` Joins all elements of an array into a string.
410-
- `arr.lastIndexOf(searchElement, fromIndex)` Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
411-
- `arr.slice(begin, end)` Extracts a section of an array and returns a new array.
412-
- `arr.toString()` Returns a string representing the array and its elements.
440+
- `array.at(index)` Returns the element at the specified index in the array.
441+
- `array.concat(value1, value2, array2)` Returns a new array comprised of this array joined with other array(s) and/or value(s).
442+
- `array.includes(searchElement, fromIndex)` Determines whether an array contains a certain element, returning true or false as appropriate.
443+
- `array.indexOf(searchElement[, fromIndex])` Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
444+
- `array.join(separator)` Joins all elements of an array into a string.
445+
- `array.lastIndexOf(searchElement, fromIndex)` Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
446+
- `array.slice(begin, end)` Extracts a section of an array and returns a new array.
447+
- `array.toString()` Returns a string representing the array and its elements.
413448
- Overrides the Object.prototype.toString() method.
414-
- `arr.toLocaleString(locales, options)` Returns a localized string representing the array and its elements.
449+
- `array.toLocaleString(locales, options)` Returns a localized string representing the array and its elements.
415450
- Overrides the `Object.prototype.toLocaleString()` method.
416451

452+
### Example of array.slice(begin, end)
453+
454+
```javascript
455+
// Suppose we have an array of months
456+
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
457+
458+
// We want to get only the spring months
459+
const springMonths = months.slice(2, 5);
460+
461+
console.log(springMonths); // Output: ['March', 'April', 'May']
462+
```
463+
417464
### Iteration Methods
418-
- `arr.every(callback[, thisArg])` Returns true if every element in this array satisfies the provided testing function.
419-
- `arr.filter(callback[, thisArg])` Creates a new array with all of the elements of this array for which the provided filtering function returns true.
420-
- `arr.find(callback[, thisArg])` Returns the found value in the array, if an element in the array satisfies the provided testing function or undefined if not found.
421-
- `arr.findIndex(callback[, thisArg])` Returns the found index in the array, if an element in the array satisfies the provided testing function or -1 if not found.
422-
- `arr.forEach(callback[, thisArg])` Calls a function for each element in the array.
423-
- `arr.keys()` Returns a new Array Iterator that contains the keys for each index in the array.
424-
- `arr.map(callback[, initialValue])` Creates a new array with the results of calling a provided function on every element in this array.
425-
- `arr.reduce(callback[, initialValue])` Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.
426-
- `arr.reduceRight(callback[, initialValue])` Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value.
427-
- `arr.some(callback[, initialValue])` Returns true if at least one element in this array satisfies the provided testing function.
428-
- `arr.values()` Returns a new Array Iterator object that contains the values for each index in the array.
465+
- `array.every(callback[, thisArg])` Returns true if every element in this array satisfies the provided testing function.
466+
- `array.filter(callback[, thisArg])` Creates a new array with all of the elements of this array for which the provided filtering function returns true.
467+
- `array.find(callback[, thisArg])` Returns the found value in the array, if an element in the array satisfies the provided testing function or undefined if not found.
468+
- `array.findIndex(callback[, thisArg])` Returns the found index in the array, if an element in the array satisfies the provided testing function or -1 if not found.
469+
- `array.forEach(callback[, thisArg])` Calls a function for each element in the array.
470+
- `array.keys()` Returns a new Array Iterator that contains the keys for each index in the array.
471+
- `array.map(callback[, initialValue])` Creates a new array with the results of calling a provided function on every element in this array.
472+
- `array.reduce(callback[, initialValue])` Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.
473+
- `array.reduceRight(callback[, initialValue])` Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value.
474+
- `array.some(callback[, initialValue])` Returns true if at least one element in this array satisfies the provided testing function.
475+
- `array.values()` Returns a new Array Iterator object that contains the values for each index in the array.
476+
477+
### Example of array.reduce(callback[, initialValue])
478+
479+
```javascript
480+
// Suppose we have an array of numbers
481+
const numbers = [1, 2, 3, 4, 5];
482+
483+
// We want to find the sum of all numbers in the array
484+
const sum = numbers.reduce((accumulator, currentValue) => {
485+
return accumulator + currentValue;
486+
}, 0); // 0 is the initial value
487+
488+
console.log(sum); // Output: 15
489+
```
429490

430491
[🔝 Back to Top](#top)
431492
---
@@ -468,7 +529,36 @@ Overview of JavaScript Object, its properties, and methods.
468529
- `Object.setPrototypeOf(obj, prototype)` Sets the prototype (i.e., the internal [[Prototype]] property).
469530
- `Object.values(obj)` Returns an array of a given object's own enumerable property values.
470531

532+
### Example of object.assign()
533+
534+
```javascript
535+
// Create a target object
536+
const target = {
537+
name: 'John',
538+
age: 30
539+
};
540+
541+
// Create source objects
542+
const source1 = {
543+
age: 25,
544+
profession: 'Engineer'
545+
};
546+
547+
const source2 = {
548+
city: 'New York',
549+
hobby: 'Guitar'
550+
};
551+
552+
// Use Object.assign to copy values from sources to the target
553+
Object.assign(target, source1, source2);
554+
555+
// Display the modified target object
556+
console.log(target);
557+
```
558+
471559
### Looping Through Properties Using for...in
560+
for...in loops through an object's properties to access and work with each property and its associated value.
561+
472562

473563
```javascript
474564
for (const key in myObject) {

0 commit comments

Comments
 (0)