Examples: Modify Results Function

Example 1

Add a new property 'totalPoints' to 'data', which is the sum of points of all users in all collections.

function(data) {
  var totalPoints = 0;
  for(var collection in data.results) {
    data.results[collection].forEach(function(row) {
      var pts = row.Points.split(' ')[0];
      totalPoints += parseInt(pts);
    });
  };
  data.totalPoints = totalPoints;
  return data;
}

Before

  {
    "name": "sample_input",
    "results": {
      "collection1": [
        { "ID": "1.", "Points": "329 points" },
        { "ID": "2.", "Points": "171 points" },
        { "ID": "3.", "Points": "145 points" }
      ],
      "collection2": [
        { "ID": "4.", "Points": "292 points" },
        { "ID": "5.", "Points": "142 points" },
        { "ID": "6.", "Points": "314 points" }
      ]
    }
  }

After

{
  "name": "sample_input",
  "results": {
    "collection1": [
      { "ID": "1.", "Points": "329 points" },
      { "ID": "2.", "Points": "171 points" },
      { "ID": "3.", "Points": "145 points" }
    ],
    "collection2": [
      { "ID": "4.", "Points": "292 points" },
      { "ID": "5.", "Points": "142 points" },
      { "ID": "6.", "Points": "314 points" }
    ]
  },
  "totalPoints": 1391
}

 

Example 2

Augment each row with the the difference of Points between January and February.

function(data) {
  function calcDiff(user) { 
    user['Diff'] = user['Points_Feb'] - user['Points_Jan'];
    return row;
  }

  for(var collection in data.results) {
    data.results[collection] = data.results[collection].map(calcDiff);
  };

  return data;
}

Before

data = {
  "name": "sample_input",
  "results": {
    "collection1": [
      { "ID": "1.", "Points_Jan": 329, "Points_Feb": 188 },
      { "ID": "2.", "Points_Jan": 171, "Points_Feb": 321 },
      { "ID": "3.", "Points_Jan": 145, "Points_Feb": 211 }
    ],
    "collection2": [
      { "ID": "4.", "Points_Jan": 292, "Points_Feb": 461 },
      { "ID": "5.", "Points_Jan": 142, "Points_Feb": 131 },
      { "ID": "6.", "Points_Jan": 314, "Points_Feb": 410 }
    ]
  }
};

After

data = {
  "name": "sample_input",
  "results": {
    "collection1": [
      { "ID": "1.", "Points_Jan": 329, "Points_Feb": 188, "Diff": -141 },
      { "ID": "2.", "Points_Jan": 171, "Points_Feb": 321, "Diff": 150 },
      { "ID": "3.", "Points_Jan": 145, "Points_Feb": 211, "Diff": 66 }
    ],
    "collection2": [
      { "ID": "4.", "Points_Jan": 292, "Points_Feb": 461, "Diff": 169 },
      { "ID": "5.", "Points_Jan": 142, "Points_Feb": 131, "Diff": -11 },
      { "ID": "6.", "Points_Jan": 314, "Points_Feb": 410, "Diff": 96 }
    ]
  }
};

 

Example 3

Sort rows in each collection from low to high based on Points.

function(data) {
  for(var collection in data.results) {
    data.results[collection].sort(function(a, b) {
      return a.Points - b.Points;
    });
  }
  return data;
}

Before

data = {
  "name": "sample_input",
  "results": {
    "collection1": [
      { "ID": "1.", "Points": 329 },
      { "ID": "2.", "Points": 171 },
      { "ID": "3.", "Points": 145 }
    ],
    "collection2": [
      { "ID": "4.", "Points": 292 },
      { "ID": "5.", "Points": 142 },
      { "ID": "6.", "Points": 314 }
    ]
  }
};

After

data = {
  "name": "sample_input",
  "results": {
    "collection1": [
      { "ID": "3.", "Points": 145 }
      { "ID": "2.", "Points": 171 },
      { "ID": "1.", "Points": 329 },
    ],
    "collection2": [
      { "ID": "5.", "Points": 142 },
      { "ID": "4.", "Points": 292 },
      { "ID": "6.", "Points": 314 }
    ]
  }
};

 

Example 4

Filter out users (i.e rows) with less than 200 Points.

function(data) {
  for(var collection in data.results) {
    data.results[collection].filter(function(user) {
      return user.Points >= 200;
    });
  };
  return data;
}

Before

data = {
  "name": "sample_input",
  "results": {
    "collection1": [
      { "ID": "1.", "Points": 329 },
      { "ID": "2.", "Points": 171 },
      { "ID": "3.", "Points": 145 }
    ],
    "collection2": [
      { "ID": "4.", "Points": 292 },
      { "ID": "5.", "Points": 142 },
      { "ID": "6.", "Points": 314 }
    ]
  }
};

After

data = {
  "name": "sample_input",
  "results": {
    "collection1": [
      { "ID": "1.", "Points": 329 }
    ],
    "collection2": [
      { "ID": "4.", "Points": 292 },
      { "ID": "6.", "Points": 314 }
    ]
  }
};
Powered by Zendesk