Değere göre nesneleri gruplama


( ! ) Notice: Undefined index: description in /home/prototurk.com/public_html/app/controller/demo.php on line 171
Call Stack
#TimeMemoryFunctionLocation
10.0002360464{main}( ).../index.php:0
20.0037442312require( '/home/prototurk.com/public_html/app/controller/demo.php' ).../index.php:101

Prototürk'e geri dön
const products = [
    {
        name: "NT1-A Condenser Mikrofon",
        brand: "Rode"
    },
    {
        name: "iPhone XS",
        brand: "Apple"
    },
    {
        name: "SmartLav Plus+ Yaka Mikrofonu",
        brand: "Rode"
    },
    {
        name: "M-AUDIO Keystation 61",
        brand: "M-Audio"
    }
]

const groupBy = (array, prop) => {
    return array.reduce((acc, obj) => {
        let key = obj[prop]
        if (!acc[key]){
            acc[key] = []
        }
        acc[key].push(obj)
        return acc
    }, {})
}

let groupedProducts = groupBy(products, 'brand')
console.log(groupedProducts)