如何使用唯一键和唯一值对创建对象

时间:2016-07-14 08:51:49

标签: javascript arrays json object

我想通过迭代一个对象数组来创建具有Key和unique值的对象。

我的Json是

var data = [
            {sex: 'male', beaconKey: '121', userKey: '01', key:'k1', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 20, preferenceList: ['Fashion','Business','Sports'], ageGroup: 'LESS_THAN_15'}
            , {sex: 'male', beaconKey: '122', userKey: '01', key:'k2', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 22, preferenceList: ['Business'], ageGroup: 'BETWEEN_25_35'}
            , {sex: 'female', beaconKey: '123', userKey: '01', key:'k3', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 26, preferenceList: ['Housing'], ageGroup: 'BETWEEN_35_45'}
            , {sex: 'male', beaconKey: '121', userKey: '03', key:'k4', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 38, preferenceList: ['Business','Housing','Sports'], ageGroup: 'BETWEEN_25_35'}
            , {sex: 'female', beaconKey: '124', userKey: '03', key:'k5', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 20, preferenceList: ['Business','Housing','Sports'], ageGroup: 'BETWEEN_45_55'}
            , {sex: 'male', beaconKey: '125', userKey: '01', key:'k6', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 42, preferenceList: ['Jewels','Kids'], ageGroup: 'LESS_THAN_15'}
            , {sex: 'female', beaconKey: '123', userKey: '02', key:'k7', date: 'Tue Jul 05 06:26:59 UTC 2016', freq: 34, preferenceList: ['Movies'], ageGroup: 'BETWEEN_55_65'}
            , {sex: 'female', beaconKey: '121', userKey: '04', key:'k8', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 22, preferenceList: ['Culture'], ageGroup: 'BETWEEN_25_35'}
            , {sex: 'female', beaconKey: '124', userKey: '01', key:'k9', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 115, preferenceList: ['Kids'], ageGroup: 'BETWEEN_35_45'}
            , {sex: 'male', beaconKey: '122', userKey: '02', key:'k10', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 19, preferenceList: [], ageGroup: 'BETWEEN_45_55'}
            , {sex: 'female', beaconKey: '121',userKey: '01', key:'k11', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 24, preferenceList: [], ageGroup: 'BETWEEN_35_45'}
        ];

我可以从上面的json中获得另一个对象

var filterObj = {
                sex:[],
                age:[],
                category:[]
            };

并且具有JSON中出现的唯一值,我也有preferenceList的多个值,这些值在filterObj中也应该是唯一的。

像我的结果一样

filterObj = {
                sex:['male','female'],
                age:['LESS_THAN_15','BETWEEN_25_35','BETWEEN_35_45','BETWEEN_45_55','BETWEEN_55_65'],
                category:['Fashion','Business','Sports','Housing','Jewels','Kids','Movies','Kids']
            };

5 个答案:

答案 0 :(得分:3)

您基本上必须迭代数组data。在数组中查找唯一值的最佳方法是使用key, value数据结构(哈希表),如Javascript对象。

var sex = {};
var age = {};

data.forEach(function (obj, index) {    
    sex[obj.sex] = index;
    age[obj.age] = index;
});

var filterObj = {};
filterObj['sex'] = Object.keys(sex);
filterObj['age'] = Object.keys(age);

答案 1 :(得分:3)

使用javascript中唯一的键和值对创建对象

function getMapObject(dataList, key) {
var lookup = {};
for ( var i in dataList) {

    var tempArr = [];
    if (lookup[dataList[i][key]] != undefined) {
        tempArr = lookup[dataList[i][key]];
        tempArr.push(dataList[i]);
        lookup[dataList[i][key]] = tempArr;
    } else {
        tempArr.push(dataList[i]);
        lookup[dataList[i][key]] = tempArr;
    }
}
return lookup;}

答案 2 :(得分:1)

您可以将对象用作已插入项目的哈希表到过滤结果。

此提案使用所需属性的地图。它很容易扩展,只需添加另一个地图名称作为属性,将数据的原始属性添加为值。

{
    "sex": "sex",
    "age": "ageGroup",
    "category": "preferenceList"
}

var data = [{ sex: 'male', beaconKey: '121', userKey: '01', key: 'k1', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 20, preferenceList: ['Fashion', 'Business', 'Sports'], ageGroup: 'LESS_THAN_15' }, { sex: 'male', beaconKey: '122', userKey: '01', key: 'k2', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 22, preferenceList: ['Business'], ageGroup: 'BETWEEN_25_35' }, { sex: 'female', beaconKey: '123', userKey: '01', key: 'k3', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 26, preferenceList: ['Housing'], ageGroup: 'BETWEEN_35_45' }, { sex: 'male', beaconKey: '121', userKey: '03', key: 'k4', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 38, preferenceList: ['Business', 'Housing', 'Sports'], ageGroup: 'BETWEEN_25_35' }, { sex: 'female', beaconKey: '124', userKey: '03', key: 'k5', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 20, preferenceList: ['Business', 'Housing', 'Sports'], ageGroup: 'BETWEEN_45_55' }, { sex: 'male', beaconKey: '125', userKey: '01', key: 'k6', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 42, preferenceList: ['Jewels', 'Kids'], ageGroup: 'LESS_THAN_15' }, { sex: 'female', beaconKey: '123', userKey: '02', key: 'k7', date: 'Tue Jul 05 06:26:59 UTC 2016', freq: 34, preferenceList: ['Movies'], ageGroup: 'BETWEEN_55_65' }, { sex: 'female', beaconKey: '121', userKey: '04', key: 'k8', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 22, preferenceList: ['Culture'], ageGroup: 'BETWEEN_25_35' }, { sex: 'female', beaconKey: '124', userKey: '01', key: 'k9', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 115, preferenceList: ['Kids'], ageGroup: 'BETWEEN_35_45' }, { sex: 'male', beaconKey: '122', userKey: '02', key: 'k10', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 19, preferenceList: [], ageGroup: 'BETWEEN_45_55' }, { sex: 'female', beaconKey: '121', userKey: '01', key: 'k11', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 24, preferenceList: [], ageGroup: 'BETWEEN_35_45' }],
    map = { sex: 'sex', age: 'ageGroup', category: 'preferenceList' },
    filterObj = {};

data.forEach(function (a) {
    Object.keys(map).forEach(function (k) {
        filterObj[k] = filterObj[k] || [];
        this[k] = this[k] || {};
        (Array.isArray(a[map[k]]) ? a[map[k]] : [a[map[k]]]).forEach(function (b) {
            if (!this[k][b]) {
                this[k][b] = true;
                filterObj[k].push(b);
            }
        }, this);
    }, this);
}, {});

console.log(filterObj);

答案 3 :(得分:0)

试试这个

var data = [
            {sex: 'male', beaconKey: '121', userKey: '01', key:'k1', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 20, preferenceList: ['Fashion','Business','Sports'], ageGroup: 'LESS_THAN_15'}
            , {sex: 'male', beaconKey: '122', userKey: '01', key:'k2', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 22, preferenceList: ['Business'], ageGroup: 'BETWEEN_25_35'}
            , {sex: 'female', beaconKey: '123', userKey: '01', key:'k3', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 26, preferenceList: ['Housing'], ageGroup: 'BETWEEN_35_45'}
            , {sex: 'male', beaconKey: '121', userKey: '03', key:'k4', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 38, preferenceList: ['Business','Housing','Sports'], ageGroup: 'BETWEEN_25_35'}
            , {sex: 'female', beaconKey: '124', userKey: '03', key:'k5', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 20, preferenceList: ['Business','Housing','Sports'], ageGroup: 'BETWEEN_45_55'}
            , {sex: 'male', beaconKey: '125', userKey: '01', key:'k6', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 42, preferenceList: ['Jewels','Kids'], ageGroup: 'LESS_THAN_15'}
            , {sex: 'female', beaconKey: '123', userKey: '02', key:'k7', date: 'Tue Jul 05 06:26:59 UTC 2016', freq: 34, preferenceList: ['Movies'], ageGroup: 'BETWEEN_55_65'}
            , {sex: 'female', beaconKey: '121', userKey: '04', key:'k8', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 22, preferenceList: ['Culture'], ageGroup: 'BETWEEN_25_35'}
            , {sex: 'female', beaconKey: '124', userKey: '01', key:'k9', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 115, preferenceList: ['Kids'], ageGroup: 'BETWEEN_35_45'}
            , {sex: 'male', beaconKey: '122', userKey: '02', key:'k10', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 19, preferenceList: [], ageGroup: 'BETWEEN_45_55'}
            , {sex: 'female', beaconKey: '121',userKey: '01', key:'k11', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 24, preferenceList: [], ageGroup: 'BETWEEN_35_45'}
        ];

var filterObj = {  sex:[], age:[], category:[] };

//iterate all objects in data
data.forEach( function( obj ){
  //check if value is already there in the array before putting the same in filterObj
  filterObj.sex.indexOf( obj.sex ) == -1 && filterObj.sex.push( obj.sex );
  filterObj.age.indexOf( obj.ageGroup ) == -1 && filterObj.age.push( obj.ageGroup );
  filterObj.category = filterObj.category.concat( obj.preferenceList ) ;
});

//find unique categories
var map = {}; 
filterObj.category.forEach( function(cat){ map[cat] = true }); 
filterObj.category = Object.keys( map );

console.log( filterObj );

答案 4 :(得分:0)

我建议你使用lodash库:

var data = [
        {sex: 'male', beaconKey: '121', userKey: '01', key:'k1', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 20, preferenceList: ['Fashion','Business','Sports'], ageGroup: 'LESS_THAN_15'}
        , {sex: 'male', beaconKey: '122', userKey: '01', key:'k2', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 22, preferenceList: ['Business'], ageGroup: 'BETWEEN_25_35'}
        , {sex: 'female', beaconKey: '123', userKey: '01', key:'k3', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 26, preferenceList: ['Housing'], ageGroup: 'BETWEEN_35_45'}
        , {sex: 'male', beaconKey: '121', userKey: '03', key:'k4', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 38, preferenceList: ['Business','Housing','Sports'], ageGroup: 'BETWEEN_25_35'}
        , {sex: 'female', beaconKey: '124', userKey: '03', key:'k5', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 20, preferenceList: ['Business','Housing','Sports'], ageGroup: 'BETWEEN_45_55'}
        , {sex: 'male', beaconKey: '125', userKey: '01', key:'k6', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 42, preferenceList: ['Jewels','Kids'], ageGroup: 'LESS_THAN_15'}
        , {sex: 'female', beaconKey: '123', userKey: '02', key:'k7', date: 'Tue Jul 05 06:26:59 UTC 2016', freq: 34, preferenceList: ['Movies'], ageGroup: 'BETWEEN_55_65'}
        , {sex: 'female', beaconKey: '121', userKey: '04', key:'k8', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 22, preferenceList: ['Culture'], ageGroup: 'BETWEEN_25_35'}
        , {sex: 'female', beaconKey: '124', userKey: '01', key:'k9', date: 'Tue Jul 05 11:59:28 UTC 2016', freq: 115, preferenceList: ['Kids'], ageGroup: 'BETWEEN_35_45'}
        , {sex: 'male', beaconKey: '122', userKey: '02', key:'k10', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 19, preferenceList: [], ageGroup: 'BETWEEN_45_55'}
        , {sex: 'female', beaconKey: '121',userKey: '01', key:'k11', date: 'Wed Jul 06 06:26:59 UTC 2016', freq: 24, preferenceList: [], ageGroup: 'BETWEEN_35_45'}
    ];

var filterObj = {
  sex: _.uniq(_.map(data,'sex')),
  age: _.uniq(_.map(data,'ageGroup')),
  category: _.union.apply(_,_.map(data,'preferenceList'))
};

这里有plnkdr:

http://plnkr.co/edit/SStCvyYdSYbmBi8xzX1a?p=preview