-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_reduce.js
More file actions
31 lines (27 loc) · 810 Bytes
/
Copy pathexample_reduce.js
File metadata and controls
31 lines (27 loc) · 810 Bytes
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
const users=[
{FirstName:"Anant",LastName:"Singh",age:90},
{FirstName:"Anuchar",LastName:"Khatri",age:89},
{FirstName:"Anmol",LastName:"Ratan",age:70},
{FirstName:"Tini",LastName:"Babu",age:70},
{FirstName:"Maharaj",LastName:"Singh",age:76}
]
const output=users.reduce(function(acc,curr){
if(acc[curr.age]){
acc[curr.age]=++acc[curr.age];
}
else{
acc[curr.age]=1;
}
return acc;
},{});
console.log(output);
// Find out the names whose age is greater than 70
// const output1=users.reduce(function(acc,curr){
// if(curr.age>70){
// acc.push(curr.FirstName);
// }
// return acc;
// },[])
// console.log(output1);
const output1=users.filter((x)=>x.age>70).map((x)=>x.FirstName); //Use both filter as well as map
console.log(output1);