Array.prototype.orderby=function() { let self=this; let arr=[self[0]], i=1; for(;i<self.length; i++) { let index=arr.findIndex(f=>f > self[i]); arr.splice(index==-1?arr.length: index, 0, self[i]); } return arr; } Array.prototype.orderdescby=function() { let self=this; let arr=[self[0]], i=1; for(;i<self.length; i++) { let index=arr.findIndex(f=>f < self[i]); arr.splice(index==-1?arr.length:index,0,self[i]); } return arr; } const points = [40, 100, 1, 500, 25, 107,0]; console.log(points.orderby()) console.log(points.orderdescby())
Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts
Thursday, July 18, 2024
Using Orderby ascending and Orderby descending to sort array javascript
Tags
Array
,
ascending
,
descending
,
JavaScript
,
orderby
,
sort
Monday, July 15, 2024
Differences between forEach(), some(), and every() in JavaScript when working with arrays
forEach():
The forEach() method iterates over each element in an array and executes a provided function for each element. It does not return a new array; instead, it performs side effects (e.g., modifying elements, logging, etc.). Use forEach() when you want to perform an action on each element without creating a new array.
Example: JavaScript
some():
The some() method checks whether at least one element in the array satisfies a given condition (predicate). It returns true if any element passes the test; otherwise, it returns false. Use some() when you want to check if any element meets a specific condition.
Example: JavaScript
every():
The every() method checks whether all elements in the array satisfy a given condition (predicate). It returns true if all elements pass the test; otherwise, it returns false. Use every() when you want to ensure that all elements meet a specific condition.
Example: JavaScript
In summary:
Use forEach() for side effects (e.g., logging, modifying elements).
Use some() to check if any element meets a condition.
Use every() to ensure all elements satisfy a condition.
The forEach() method iterates over each element in an array and executes a provided function for each element. It does not return a new array; instead, it performs side effects (e.g., modifying elements, logging, etc.). Use forEach() when you want to perform an action on each element without creating a new array.
Example: JavaScript
const numbers = [1, 2, 3, 4]; numbers.forEach((num) => console.log(num));
some():
The some() method checks whether at least one element in the array satisfies a given condition (predicate). It returns true if any element passes the test; otherwise, it returns false. Use some() when you want to check if any element meets a specific condition.
Example: JavaScript
const strarr = ["method", "checks", "whether"]; const isFound = strarr.some((item) => item==="whether"); console.log(isFound); // true const isNotFound = strarr.some((item) => item==="ram"); console.log(isNotFound); // false
every():
The every() method checks whether all elements in the array satisfy a given condition (predicate). It returns true if all elements pass the test; otherwise, it returns false. Use every() when you want to ensure that all elements meet a specific condition.
Example: JavaScript
const numbers = [1, 2, 3, 4]; const allPositive = numbers.every((num) => num > 0); console.log(allPositive); // true
In summary:
Use forEach() for side effects (e.g., logging, modifying elements).
Use some() to check if any element meets a condition.
Use every() to ensure all elements satisfy a condition.
Subscribe to:
Posts
(
Atom
)