pimp-my-book / pmb-plus-frontend

PMB Plus' frontends. A marketplace for second hand textbooks. 🛒
https://dgc62rzfmrsbq.cloudfront.net/
0 stars 0 forks source link

Object methods #9

Closed AmoDinho closed 5 years ago

AmoDinho commented 5 years ago

https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects

https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key/40774906

https://gist.github.com/JamieMason/0566f8412af9fe6a1d470aa1e089a752

https://codeburst.io/grouping-array-data-json-ef96b438b927

https://learnwithparam.com/blog/how-to-group-by-array-of-objects-using-a-key/

https://codeburst.io/useful-javascript-array-and-object-methods-6c7971d93230

AmoDinho commented 5 years ago
let groupByCourse = data.getBooksByCourse.reduce((acc, it) => (

        acc[it.course] = it,
        acc

    ), {})

   // let course = data.getBooksByCourse.reduce((h, obj) => Object.assign(h, { [obj.key]: (h[obj.key] || []).concat(obj) }), {})
    //console.log(groupByCourse)
    ///console.log(course)
   // const grouped = data.getBooksByCourse.reduce((acc, it) => acc.set(it.course, [...acc.get(it.course) || [], it]), new Map())

    //MDN FUNCTION
    const mdnCourse = data.getBooksByCourse.reduce((acc, obj) => {
        var key = obj[course];
        if (!acc[key]) {
            acc[key] = [];
        }
        acc[key].push(obj);
        return acc;
    }, {})

    console.log(mdnCourse)
    console.log(data.getBooksByCourse)
AmoDinho commented 5 years ago
[INF 370: Array(2), wtw09433: Array(1), LLB600: Array(1), EKN 123: Array(3), WTW 567: Array(1), …]
EKN 123: (3) [{…}, {…}, {…}]
FRK 345: [{…}]
INF 370: Array(2)
0: {ID: 1, title: "Silver Lining", grade: "1", price: "300", image: "https://upload.wikimedia.org/wikipedia/en/thumb/7/…_The_Now_Now.jpg/220px-Gorillaz_-_The_Now_Now.jpg", …}
1: {ID: 2, title: "Like an icon", grade: "2", price: "300", image: "https://upload.wikimedia.org/wikipedia/en/thumb/7/…_The_Now_Now.jpg/220px-Gorillaz_-_The_Now_Now.jpg", …}
length: 2
__proto__: Array(0)
LLB600: [{…}]
OBS 342: (15) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
STK 985: Array(15)
0: {ID: 22, title: "Meta Ele: Libro Del Alumno + Cuaderno De Ejercicios + CD A2", grade: "22", price: "776", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
1: {ID: 39, title: "Abnormal Child Psychology", grade: "39", price: "947", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
2: {ID: 40, title: "Dynamics of Structures", grade: "40", price: "523", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
3: {ID: 41, title: "Structural Analysis", grade: "41", price: "423", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
4: {ID: 42, title: "Statics and Mechanics of Materials, SI Edition", grade: "42", price: "779", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
5: {ID: 43, title: "Facilitating Language Learning in the Foundation Phase", grade: "43", price: "595", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
6: {ID: 44, title: "Facilitating Language Learning in the Foundation Phase", grade: "44", price: "266", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
7: {ID: 45, title: "Geletterdheid in die grondslagfase", grade: "45", price: "522", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
8: {ID: 46, title: "Literacy in the foundation phase", grade: "46", price: "456", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
9: {ID: 47, title: "Literacy in the foundation phase", grade: "47", price: "123", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
10: {ID: 48, title: "Literacy in the foundation phase", grade: "48", price: "838", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
11: {ID: 49, title: "Introducing children's literature : A guide to the South African classroom", grade: "49", price: "259", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
12: {ID: 50, title: "Introduction to Quantum Mechanics", grade: "50", price: "492", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
13: {ID: 51, title: "Introduction to Quantum Mechanics", grade: "51", price: "300", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
14: {ID: 52, title: "Ethics for accountants & auditors", grade: "52", price: "602", image: "https://dummyimage.com/200x200/000/0afcf4.png&text=PMB+Plus", …}
length: 15
__proto__: Array(0)
WTW 567: [{…}]
wtw09433: [{…}]
length: 0
__proto__: Array(0)