-
Notifications
You must be signed in to change notification settings - Fork 77
/
10_arrayOfObjects.js
79 lines (71 loc) · 1.66 KB
/
10_arrayOfObjects.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
let arr = [
"Bharat",
21,
{
name: "Bharat",
age: 21,
cities: [
"Delhi",
"Mumbai",
"Bangalore",
{
country: "India",
population: 1500000000,
},
],
},
];
console.log(arr[0]); // Bharat
console.log(arr[1]); // 21
console.log(arr[2]); // { name: 'Bharat', age: 21, cities: [ 'Delhi', 'Mumbai', 'Bangalore', { country: 'India', population: 1500000000 } ] }
console.log(arr[2].name); // Bharat
console.log(arr[2].age); // 21
console.log(arr[2].cities); // [ 'Delhi', 'Mumbai', 'Bangalore', { country: 'India', population: 1500000000 } ]
console.log(arr[2].cities[0]); // Delhi
console.log(arr[2].cities[1]); // Mumbai
console.log(arr[2].cities[2]); // Bangalore
console.log(arr[2].cities[3]); // { country: 'India', population: 1500000000 }
console.log(arr[2].cities[3].country); // India
console.log(arr[2].cities[3].population); // 1500000000
// Create a function that takes an array of objects as an 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",
},
];
// for (let i = 0; i < users.length; i++) {
// console.log(users[i]);
// }
function getEligibleUsers(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 = getEligibleUsers(users);
console.log(allUsers);