在下面的數據集中,我要計算關於每個bKey的aKey數。 這樣每個bKey有多少個唯一的aKey。就像是如果某個aKey已經發生,那麼對於那個bKey就不應該被計數。相對於對象數組中的另一個屬性的唯一屬性發生次數
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'}
];
我想要的結果是在對象的新的磁盤陣列作爲
[ {121: 3},{122: 2},{123: 2},{124: 2},{125: 1} ]
我想這一點 -
var dataSet = {};
fData2.forEach(function (item) {
dataSet[ item.bKey ] = (dataSet[ item.aKey ] || 0) + 1;
});
console.log(dataSet);
ohh是的,抱歉是錯誤的。你說得對,就是item.bKey。 'var dataSet = {}; (item){ dataSet [item.bKey] =(dataSet [item.userKey] || 0)+ 1; }); console.log(dataSet);' – Raichu
也請把aKey當作userKey。我無法編輯問題。 – Raichu
你確定結果不是'3 2 2 1 1'嗎? –