-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy path11_objectOfObjects.js
More file actions
61 lines (54 loc) · 1.1 KB
/
11_objectOfObjects.js
File metadata and controls
61 lines (54 loc) · 1.1 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
const user = {
name: "Bharat",
age: 21,
address: {
city: "Samastipur",
country: "India",
address: "xyz abc",
},
};
console.log(user.name); // Bharat
console.log(user.age); // 21
console.log(user.address); // { city: 'Samastipur', country: 'India', address: 'xyz abc' }
console.log(user.address.city); // Samastipur
console.log(user.address.country); // India
console.log(user.address.address); // xyz abc
// 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",
},
];
function getUsers(users) {
let ans = [];
for (let i = 0; i < users.length; i++) {
if (users[i].age > 18 && users[i].gender === "male") {
ans.push(users[i]);
}
}
return ans;
}
let allUsers = getUsers(users);
console.log(allUsers);