forked from sudheerj/javascript-interview-questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuperArrayOfObjects.js
More file actions
42 lines (35 loc) · 1013 Bytes
/
superArrayOfObjects.js
File metadata and controls
42 lines (35 loc) · 1013 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
32
33
34
35
36
37
38
39
40
41
42
// Example data
const aob =
[
{ framework: 'React.JS', website: 'Paypal' },
{ framework: 'React.JS', website: 'Tesla' },
{ framework: 'Angular', website: 'Google' },
{ framework: 'Vue.JS', website: 'Vue' },
{ framework: 'JavaScript', website: 'inblack67' },
]
const superAob = (data, victim) => {
const obj = {};
data.forEach((data) => {
if(data.hasOwnProperty(victim)){
if(obj[data[victim]]){
obj[data[victim]]++;
}
else{
obj[data[victim]] = 1;
}
}
})
let superArrayOfObjects = [];
for (const key in obj) {
superArrayOfObjects = [...superArrayOfObjects, { victim: key, count: obj[key]}];
}
return superArrayOfObjects;
}
console.log(superAob(aob, 'framework'));
// output:-
// [
// { victim: 'React.JS', count: 2 },
// { victim: 'Angular', count: 1 },
// { victim: 'Vue.JS', count: 1 },
// { victim: 'JavaScript', count: 1 }
// ]