daneillone
daneillone

Reputation: 95

Reduce function with bracket key

I'm confused what the following line of code means:

if(!acc[key])

This is my interpretation of the line:

If the key is not in acc then set the key with value array and jump out of the if statement. Then push the obj in acc key value.

In case the key is in acc, skip the if statement and use another memory acc[key] and set the key which is in acc and set value with obj. (NEEDS MAJOR REVISION)

Is my explanation correct?

var people = [{
    name: 'Alice',
    age: 21
  },
  {
    name: 'Max',
    age: 20
  },
  {
    name: 'Jane',
    age: 20
  }
];

function groupBy(objectArray, property) {
  return objectArray.reduce(function(acc, obj) {
    var key = obj[property];
    if (!acc[key]) {
      acc[key] = [];
      acc[key].push(obj)
    }
    return acc;

  }, {});
}
var groupedPeople = groupBy(people, 'age')
console.log(JSON.stringify(groupedPeople))

Upvotes: 4

Views: 567

Answers (3)

janvee gajera
janvee gajera

Reputation: 1

    "HR": [
        {
            name: janki
            sal: 50000
        },
        {
            name: er
            sal: 47852
        },
        {
            name: sfd
            sal: 25685
        }
    ],
    "Management": [
        {
            name: janki
            sal: 50000
        },
        {
            name: er
            sal: 47852
        },
        {
            name: sfd
            sal: 25685
        }

Upvotes: 0

tam.dangc
tam.dangc

Reputation: 2032

The !acc[key] mean it will return false if the property key (it is dynamic like @Jack Bashford said) doesn't exist in object acc. The reduce will work

  1. acc init with {}
  2. loop into the first object: key = '21', but acc['21'] is undefined run the block code of if
  3. 2nd: key = '20' and !acc[key] return true ~> set acc['20'] = obj
  4. last object. key = '20', but acc['20'] has value at step 3 ~> skip block code and return acc
  5. return acc with 2 key '20' and '21'

The reason the result starts with 20 first is I think the browser want to print the property of the object which sorted by the alphabet.

Upvotes: 1

Jack Bashford
Jack Bashford

Reputation: 44125

The square brackets [] are used for dynamic property notation (also known as computed property names). It's like this:

var obj = { foo: "bar" };
var propertyWeWant = "foo"; //We want to get obj.foo - the value "bar"
console.log(obj.propertyWeWant); //Returns undefined
console.log(obj[propertyWeWant]); //Returns bar

The function groupBy takes an array of people, and returns the people grouped by the passed property (in this case, age):

var people = [{
    name: 'Alice',
    age: 21
  },
  {
    name: 'Max',
    age: 20
  },
  {
    name: 'Jane',
    age: 20
  }
];

function groupBy(objectArray, property) {
  return objectArray.reduce(function(acc, obj) {
    var key = obj[property];
    if (!acc[key]) {
      acc[key] = [];
    }
    acc[key].push(obj)
    return acc;

  }, {});
}
var groupedPeople = groupBy(people, 'age')
console.log(JSON.stringify(groupedPeople))

Upvotes: 3

Related Questions