0

hi all i am using javascript i have a set of array i need to group by the object

code

 var data = [{BuildingID: "5", FloorId: "65", one: 12, two: 15,three: 12},
            {BuildingID: "5", FloorId: "65", one: 12, two: 15,three: 12},
            {BuildingID: "6", FloorId: "65", one: 12, two: 15,three: 12},
            {BuildingID: "6", FloorId: "65", one: 12, two: 15,three: 12}]

i am trying to do group by the BuildingID add one,two,three NOTE:one two three is not static

Excepted Output

  var data = [{BuildingID: "5", FloorId: "65", one: 24, two: 30,three: 24},
              {BuildingID: "6", FloorId: "65", one: 24, two: 30,three: 24} ]
Community
  • 1
  • 1
jose
  • 1,044
  • 1
  • 12
  • 35

3 Answers3

2

You could use a dynamic approach by using a hash table and an array for the static keys of the object.

var data = [{ BuildingID: "5", FloorId: "65", one: 12, two: 15, three: 12 }, { BuildingID: "5", FloorId: "65", one: 12, two: 15, three: 12 }, { BuildingID: "6", FloorId: "65", one: 12, two: 15, three: 12 }, { BuildingID: "6", FloorId: "65", one: 12, two: 15, three: 12 }],
    staticKeys = ['BuildingID', 'FloorId'] ,
    grouped = data.reduce(function (hash) {
        return function (r, a) {
            var key = a[staticKeys[0]];
            if (!hash[key]) {
                hash[key] = {};
                staticKeys.forEach(function (k) {
                    hash[key][k] = a[k];
                });
                r.push(hash[key]);
            }
            Object.keys(a).forEach(function (k) {
                if (staticKeys.indexOf(k) === -1) {
                    hash[key][k] = (hash[key][k] || 0) + a[k];
                }
            });
            return r;
        };
    }(Object.create(null)), []);

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

here is a snippet code that work for me,

it assums you know the key attribute of the object

var arr = [ { id: 1, name: 'bob' }, { id: 1, name: 'bill' }, { id: 1, name: 'bill' } ]

var noDuplicate = [];
var unique = {};

$.each(arr, function(key, item) {

    if (! unique[item.id + "-" + item.name]) {
        noDuplicate.push(item);
        unique[item.id + "-" + item.name] = true;
    }
});

console.log(noDuplicate);

hope it helps ;)

regards.

fxlacroix
  • 557
  • 4
  • 18
1

You can try this snippet, the "ouput" variable contains the result;

var data = [{ BuildingID: "5", FloorId: "65", one: 12, two: 15, three: 12 }, { BuildingID: "5", FloorId: "65", one: 12, two: 15, three: 12 }, { BuildingID: "6", FloorId: "65", one: 12, two: 15, three: 12 }, { BuildingID: "6", FloorId: "65", one: 12, two: 15, three: 12 }]
var groupBy = function(input, key) {
   return input.reduce(function(list, x) {
     list[x[key]] = x;
     return list;
   }, {});
};
var grouped = groupBy(data, 'BuildingID'), output=[];
for ( var key in grouped ) { output[output.length] = grouped[key]; }
console.log(output);
Arno
  • 1,309
  • 14
  • 20