Raichu Raichu - 5 months ago 12
Javascript Question

Count No.of unique attribute occurances with respect to another attribute in Array of objects

In the below data set, I want to count number of aKey with respect to each bKey.
Such that for every bKey how many unique aKey are there. Like if a certain aKey is occurred already then for that bKey it should not be counted.

var fData2 = [
{sex: 'male', bKey: '121', userKey: '04', aKey: '6e5fdb09-319f-4640-b241-2b12d350c027', date: 'Fri Jul 01 06:26:59 UTC 2016', freq: 20, preferenceList: ['Fashion','Business','Sports'], ageGroup: 'LESS_THAN_15'},
{sex: 'male', bKey: '122', userKey: '01', aKey: '6e5fdb09-319f-4640-b241-2b12d350c027', date: 'Fri Jul 01 11:59:28 UTC 2016', freq: 22, preferenceList: ['Business'], ageGroup: 'BETWEEN_25_35'},
{sex: 'female', bKey: '123', userKey: '01', aKey: '6e5fdb09-319f-4640-b241-2b12d350c027', date: 'Thu Jun 30 11:59:28 UTC 2016', freq: 26, preferenceList: ['Housing'], ageGroup: 'BETWEEN_35_45'},
{sex: 'male', bKey: '121', userKey: '03', aKey: '4a73c208-43a8-49d2-87ba-f44691786c96', date: 'Fri Jul 01 06:26:59 UTC 2016', freq: 38, preferenceList: ['Business','Housing','Sports'], ageGroup: 'BETWEEN_25_35'},
{sex: 'female', bKey: '124', userKey: '03', aKey: '4a73c208-43a8-49d2-87ba-f44691786c96', date: 'Fri Jul 01 11:59:28 UTC 2016', freq: 20, preferenceList: ['Business','Housing','Sports'], ageGroup: 'BETWEEN_45_55'},
{sex: 'male', bKey: '125', userKey: '01', aKey: '4a73c208-43a8-49d2-87ba-f44691786c96', date: 'Fri Jul 01 11:59:28 UTC 2016', freq: 42, preferenceList: ['Jewels','Kids'], ageGroup: 'LESS_THAN_15'},
{sex: 'female', bKey: '123', userKey: '02', aKey: '4a73c208-43a8-49d2-87ba-f44691786c96', date: 'Fri Jul 01 06:26:59 UTC 2016', freq: 34, preferenceList: ['Movies'], ageGroup: 'BETWEEN_55_65'},
{sex: 'female', bKey: '121', userKey: '03', aKey: '4a73c208-43a8-49d2-87ba-f44691786c96', date: 'Fri Jul 01 06:26:59 UTC 2016', freq: 22, preferenceList: ['Culture'], ageGroup: 'BETWEEN_25_35'},
{sex: 'female', bKey: '124', userKey: '01', aKey: '4a73c208-43a8-49d2-87ba-f44691786c96', date: 'Fri Jul 01 11:59:28 UTC 2016', freq: 115, preferenceList: ['Kids'], ageGroup: 'BETWEEN_35_45'},
{sex: 'male', bKey: '122', userKey: '02', aKey: '09a703f3-2273-4b25-ba33-24572ffcdf89', date: 'Thu Jun 30 11:59:28 UTC 2016', freq: 19, preferenceList: [], ageGroup: 'BETWEEN_45_55'},
{sex: 'female', bKey: '121',userKey: '01', aKey: '09a703f3-2273-4b25-ba33-24572ffcdf89', date: 'Thu Jun 30 06:26:59 UTC 2016', freq: 24, preferenceList: [], ageGroup: 'BETWEEN_35_45'}
];


I want the result to be in a new Array of objects as

[ {121: 3},{122: 2},{123: 2},{124: 2},{125: 1} ]


I am trying this -

var dataSet = {};
fData2.forEach(function (item) {
dataSet[ item.bKey ] = (dataSet[ item.aKey ] || 0) + 1;
});

console.log(dataSet);

Answer

You can do this with forEach loop and Object as optional parameter

var data = [{"sex":"male","bKey":"121","userKey":"04","aKey":"6e5fdb09-319f-4640-b241-2b12d350c027","date":"Fri Jul 01 06:26:59 UTC 2016","freq":20,"preferenceList":["Fashion","Business","Sports"],"ageGroup":"LESS_THAN_15"},{"sex":"male","bKey":"122","userKey":"01","aKey":"6e5fdb09-319f-4640-b241-2b12d350c027","date":"Fri Jul 01 11:59:28 UTC 2016","freq":22,"preferenceList":["Business"],"ageGroup":"BETWEEN_25_35"},{"sex":"female","bKey":"123","userKey":"01","aKey":"6e5fdb09-319f-4640-b241-2b12d350c027","date":"Thu Jun 30 11:59:28 UTC 2016","freq":26,"preferenceList":["Housing"],"ageGroup":"BETWEEN_35_45"},{"sex":"male","bKey":"121","userKey":"03","aKey":"4a73c208-43a8-49d2-87ba-f44691786c96","date":"Fri Jul 01 06:26:59 UTC 2016","freq":38,"preferenceList":["Business","Housing","Sports"],"ageGroup":"BETWEEN_25_35"},{"sex":"female","bKey":"124","userKey":"03","aKey":"4a73c208-43a8-49d2-87ba-f44691786c96","date":"Fri Jul 01 11:59:28 UTC 2016","freq":20,"preferenceList":["Business","Housing","Sports"],"ageGroup":"BETWEEN_45_55"},{"sex":"male","bKey":"125","userKey":"01","aKey":"4a73c208-43a8-49d2-87ba-f44691786c96","date":"Fri Jul 01 11:59:28 UTC 2016","freq":42,"preferenceList":["Jewels","Kids"],"ageGroup":"LESS_THAN_15"},{"sex":"female","bKey":"123","userKey":"02","aKey":"4a73c208-43a8-49d2-87ba-f44691786c96","date":"Fri Jul 01 06:26:59 UTC 2016","freq":34,"preferenceList":["Movies"],"ageGroup":"BETWEEN_55_65"},{"sex":"female","bKey":"121","userKey":"03","aKey":"4a73c208-43a8-49d2-87ba-f44691786c96","date":"Fri Jul 01 06:26:59 UTC 2016","freq":22,"preferenceList":["Culture"],"ageGroup":"BETWEEN_25_35"},{"sex":"female","bKey":"124","userKey":"01","aKey":"4a73c208-43a8-49d2-87ba-f44691786c96","date":"Fri Jul 01 11:59:28 UTC 2016","freq":115,"preferenceList":["Kids"],"ageGroup":"BETWEEN_35_45"},{"sex":"male","bKey":"122","userKey":"02","aKey":"09a703f3-2273-4b25-ba33-24572ffcdf89","date":"Thu Jun 30 11:59:28 UTC 2016","freq":19,"preferenceList":[],"ageGroup":"BETWEEN_45_55"},{"sex":"female","bKey":"121","userKey":"01","aKey":"09a703f3-2273-4b25-ba33-24572ffcdf89","date":"Thu Jun 30 06:26:59 UTC 2016","freq":24,"preferenceList":[],"ageGroup":"BETWEEN_35_45"}]
var result = {}

data.forEach(function(e) {
  if(!this[e.bKey + e.aKey]) {
    this[e.bKey + e.aKey] = true;
    result[e.bKey] = (result[e.bKey] || 0) +1 ;
  }
}, {})

console.log(result)

Comments