forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperArrayOfObjects.js
64 lines (51 loc) · 1.52 KB
/
superArrayOfObjects.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
// 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;
}
const superAobMethodSecond = (data, victim) => {
const totalKeys = []
data.forEach(a => {
if(!totalKeys.includes(a[victim]))
{
totalKeys.push(a[victim]);
}
});
let superArrayOfObjects = [];
totalKeys.forEach((types) => {
const length = data.filter((aobject)=> aobject[victim] === types).length;
superArrayOfObjects = [...superArrayOfObjects, { victim: types, count: length}];
})
return superArrayOfObjects;
}
console.log(superAob(aob, 'framework'));
console.log(superAobMethodSecond(aob, 'framework'));
// output:-
// [
// { victim: 'React.JS', count: 2 },
// { victim: 'Angular', count: 1 },
// { victim: 'Vue.JS', count: 1 },
// { victim: 'JavaScript', count: 1 }
// ]