1. `push()` 2. `pop()` 3. `shift()` 4. `unshift()` 5. `splice()`
6. `slice()` 7. `concat()` 8. `indexOf()` 9. `includes()` 10. `forEach()`
11. `map()` 12. `filter()` 13. `reduce()` 14. `find()` 15. `sort()`
16. `reverse()` 17. `join()` 18. `some()` 19. `every()` 20. `findIndex()`
21. `fill()` 22. `copyWithin()` 23. `flat()` 24. `flatMap()` 25. `at()`
Sum of Array Elements
Calculate the sum of all elements in an array:
let numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce((acc, value) => {
return acc + value;
}, 0); // Initial value of the accumulator is 0
console.log(sum); // Output: 15
2. Flatten an Array of Arrays
Flatten a nested array into a single array:
let nestedArray = [[1, 2], [3, 4], [5, 6]];
let flattenedArray = nestedArray.reduce((acc, value) => {
return acc.concat(value);
}, []); // Initial value of the accumulator is an empty array
console.log(flattenedArray); // Output: [1, 2, 3, 4, 5, 6]
3. Count Occurrences of Elements
Count the occurrences of each element in an array:
let elements = ['a', 'b', 'a', 'c', 'b', 'a'];
let occurrences = elements.reduce((acc, value) => {
acc[value] = (acc[value] || 0) + 1;
return acc;
}, {}); // Initial value of the accumulator is an empty object
console.log(occurrences); // Output: { a: 3, b: 2, c: 1 }
4. Group Objects by Property
Group an array of objects by a specific property:
let people = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 25 }
];
let groupedByAge = people.reduce((acc, person) => {
if (!acc[person.age]) {
acc[person.age] = [];
}
acc[person.age].push(person);
return acc;
}, {}); // Initial value of the accumulator is an empty object
console.log(groupedByAge);
// Output: { 25: [{ name: 'Alice', age: 25 }, { name: 'Charlie', age: 25 }], 30: [{ name: 'Bob', age: 30 }] }
5. Calculate Product of Array Elements
Calculate the product of all elements in an array:
let numbers = [1, 2, 3, 4, 5];
let product = numbers.reduce((acc, value) => {
return acc * value;
}, 1); // Initial value of the accumulator is 1
console.log(product); // Output: 120
6. Remove Duplicates from an Array
Remove duplicates from an array:
let numbers = [1, 2, 3, 2, 4, 3, 5];
let uniqueNumbers = numbers.reduce((acc, value) => {
if (!acc.includes(value)) {
acc.push(value);
}
return acc;
}, []); // Initial value of the accumulator is an empty array
console.log(uniqueNumbers); // Output: [1, 2, 3, 4, 5]
These examples demonstrate the flexibility and power of the reduce
method in JavaScript. If you have any more specific scenarios or questions, feel free to ask!
---------------------------------------------FUNCTIONS-------------------------------------------------
1. Callback Function :
function myName(name){
console.log("hello "+name)
}
function showName(callback){
const nameValue="maheshh";
return callback(nameValue);
}
showName(myName)
Example 2:callback
This is the main message. The message has been displayed, and this is the completion callback.
Q3: Lexical Scope, Global Scope, Local Scope:
functions are easy to read, access, build and maintain.
2.Anonymous Function: A function without a name, often used as an argument to other functions.
await
No comments:
Post a Comment