-
Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy path9_objectOfObjects.js
More file actions
61 lines (53 loc) · 1.34 KB
/
9_objectOfObjects.js
File metadata and controls
61 lines (53 loc) · 1.34 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 1. Create a function that takes an array of objects as input, and returns the users whose age > 18 and are male
let users = [
{
name: "Bharat",
age: 21,
gender: "male",
},
{
name: "Priya",
age: 22,
gender: "female",
},
{
name: "Rani",
age: 15,
gender: "female",
},
{
name: "Deepak",
age: 24,
gender: "male",
},
{
name: "Rahul",
age: 17,
gender: "male",
},
];
// define a function called `getUsers` that takes an array of users as an input
function getUsers(users) {
// using filter method
// filter the users whose age > 18 and gender is male and store them in a new array
let filteredUsers = users.filter((user) => user.age > 18 && user.gender === "male");
// returns the filtered users
return filteredUsers;
/*
// using normal loop
// create an empty array to store the users whose age
let ans = [];
// using for loop to iterate through the array of users
for (let i = 0; i < users.length; i++) {
// check if the age of the user is greater than 18 and gender is male
if (users[i].age > 18 && users[i].gender === "male") {
ans.push(users[i]);
}
}
return ans;
*/
}
// calls the function `getUsers` with the array of users as an input
let allUsers = getUsers(users);
// prints the users whose age > 18 and
console.log(allUsers);