DEV Community

devsanilo
devsanilo

Posted on

How can i access single element of a grouped array nodejs?

How can i access the elements of the final Array

function groupByKey(array, key) {
        return array
          .reduce((hash, obj) => {
            if(obj[key] === undefined) return hash; 
            return Object.assign(hash, { [obj[key]]:( hash[obj[key]] || [] ).concat(obj)})
          }, {})
     }
     var cars = [{'make':'audi','model':'r8','year':'2012'},{'make':'audi','model':'rs5','year':'2013'},{'make':'ford','model':'mustang','year':'2012'},{'make':'ford','model':'fusion','year':'2015'},{'make':'kia','model':'optima','year':'2012'}];
      const v  = cars['model']
     const result = groupByKey(cars, 'make')
     console.log(result)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)