2015-01-27 7 views
2

Fiddle ExampleГруппировка вложенного массива

Я массив, порожденный nest методом в d3.js Можно ли предложить JavaScript/JQuery способ группировки объектов в values собственности category?

var data = [ 
    { 
    "key": "0", 
    "values": [ 
     { 
     "category": "phone", 
     "brand": "Sony", 
     "brand_id": "0", 
     "name": "item A", 
     "id": "551", 
     "point": "600" 
     }, 
     { 
     "category": "software", 
     "brand": "Sony", 
     "brand_id": "0", 
     "name": "item D", 
     "id": "51", 
     "point": "800" 
     }, 
     { 
     "category": "phone", 
     "brand": "Sony", 
     "brand_id": "0", 
     "name": "item E", 
     "id": "52", 
     "point": "1800" 
     } 
    ] 
    }, 
    { 
    "key": "0", 
    "values": [ 
     { 
     "category": "software", 
     "brand": "Microsoft", 
     "brand_id": "1", 
     "name": "item B", 
     "id": "54", 
     "point": "800" 
     }, 
     { 
     "category": "phone", 
     "brand": "Microsoft", 
     "brand_id": "1", 
     "name": "item B", 
     "id": "60", 
     "point": "200" 
     }, 
     { 
     "category": "phone", 
     "brand": "Microsoft", 
     "brand_id": "1", 
     "name": "item T", 
     "id": "30", 
     "point": "600" 
     }, 
     { 
     "category": "software", 
     "brand": "Microsoft", 
     "brand_id": "1", 
     "name": "item N", 
     "id": "90", 
     "point": "500" 
     } 
    ] 
    } 
]; 

Вот желаемый результат:

[ 
    { 
    "key": "0", 
    "values": [ 
     { 
     "phone": [ 
      { 
      "category": "phone", 
      "brand": "Sony", 
      "brand_id": "0", 
      "name": "item A", 
      "id": "551", 
      "point": "600" 
      }, 
      { 
      "category": "phone", 
      "brand": "Sony", 
      "brand_id": "0", 
      "name": "item E", 
      "id": "52", 
      "point": "1800" 
      } 
     ], 
     "software": [ 
      { 
      "category": "software", 
      "brand": "Sony", 
      "brand_id": "0", 
      "name": "item D", 
      "id": "51", 
      "point": "800" 
      } 
     ] 
     } 
    ] 
    }, 
    { 
    "key": "0", 
    "values": [ 
     { 
     "phone": [ 
      { 
      "category": "phone", 
      "brand": "Microsoft", 
      "brand_id": "0", 
      "name": "item B", 
      "id": "80", 
      "point": "54" 
      }, 
      { 
      "category": "phone", 
      "brand": "Microsoft", 
      "brand_id": "0", 
      "name": "item D", 
      "id": "52", 
      "point": "1800" 
      } 
     ], 
     "software": [ 
      { 
      "category": "software", 
      "brand": "Microsoft", 
      "brand_id": "0", 
      "name": "item G", 
      "id": "41", 
      "point": "800" 
      }, 
      { 
      "category": "software", 
      "brand": "Microsoft", 
      "brand_id": "0", 
      "name": "item L", 
      "id": "42", 
      "point": "900" 
      } 
     ] 
     } 
    ] 
    } 
] 

Следующий код не работает. Я думаю, это потому, что grouped[t.category] = [] определяется в каждом цикле. Предыдущий элемент будет перезаписан последним. Если бы я не определил это, я хотел бы получить grouped[t.category] неопределенную ошибку:

grouped = {}; 
data.forEach(function(d){ 
    d.values.map(function(t){ 
    grouped[t.category] = [];  
    grouped[t.category].push({name:t.name,tid:t.id,point:parseInt(t.point)}); 
    }) 
}) 

ответ

1

Попробуйте это

var grouped; 

data.forEach(function(d) { 
    grouped = {}; 

    d.values.forEach(function(t) { 
     if (!grouped[t.category]) { 
      grouped[t.category] = []; 
     } 

     grouped[t.category].push({ 
      name: t.name, 
      tid: t.id, 
      point: parseInt(t.point) 
     }); 
    }) 

    d.values = grouped; 
}); 

Example

1

Вы можете попробовать это: jsfiddle

var result = data.map(function (record) { 
    return { 
     key: record.key, 
     values: [ groupBy('category', record.values) ] 
    }; 
}) 

function groupBy (key, arr) { 
    var groups = {}; 
    arr.forEach(function (el) { 
     var keyVal = el[ key ]; 
     if (!keyVal) return; 

     if (groups[ keyVal ]) 
      groups[ keyVal ].push(el); 
     else 
      groups[ keyVal ] = [ el ]; 
    }); 
    return groups; 
} 
2

Вы можете решить эту проблему, используя lodash или подобная библиотека

_.map(data, function(x){ 
    return _.assign({}, x, {values: _.groupBy(x.values, 'category')}) 
}) 

Fiddle

1

Я знаком с underscore.js:

var desireResult = _.map(data, function(obj){ 
    obj.values = _.groupBy(obj.values, 'category'); 
    return obj; 
}); 

Надеется, что это поможет!

Смежные вопросы