javascript - Count No.of unique attribute occurances with respect to another attribute in Array of objects -
in below data set, want count number of akey respect each bkey. such every bkey how many unique akey there. if akey occurred bkey should not 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 result in new array of objects
[ {121: 3},{122: 2},{123: 2},{124: 2},{125: 1} ]
i trying -
var dataset = {}; fdata2.foreach(function (item) { dataset[ item.bkey ] = (dataset[ item.akey ] || 0) + 1; }); console.log(dataset);
you can foreach
loop , object 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
Post a Comment