-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20-Day.js
46 lines (39 loc) · 1.26 KB
/
20-Day.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function findMaxByProperty(arr, prop) {
// Check if the array is empty
if (arr.length === 0) {
return null;
}
// Initialize maxObj to first object in the array
let maxObj = arr[0];
// Loop through the array starting at second object
for (let i = 1; i < arr.length; i++) {
// Check if the current object's property value is greater than maxObj's property value
if (arr[i][prop] > maxObj[prop]) {
// If yes, update maxObj to current object
maxObj = arr[i];
}
}
// Return the object with highest value for the specified property
return maxObj;
}
// Test case 1
const arr1 = [
{ name: "apple", price: 1 },
{ name: "banana", price: 2 },
{ name: "orange", price: 3 },
];
const maxObj1 = findMaxByProperty(arr1, "price");
console.log(maxObj1); // { name: 'orange', price: 3 }
// Test case 2
const arr2 = [
{ name: "Gajedra", age: 20 },
{ name: "Nidhi", age: 21 },
{ name: "Gaurav", age: 22 },
{ name: "Dimple", age: 21 },
];
const maxObj2 = findMaxByProperty(arr2, "age");
console.log(maxObj2); // { name: 'Gaurav', age: 22 }
// Test case 3
const arr3 = [];
const maxObj3 = findMaxByProperty(arr3, "price");
console.log(maxObj3); // null