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} ]
7
  • 2
    expected result json?? Commented Jun 6, 2017 at 12:35
  • 1
    what should happen with one, two, three? Commented Jun 6, 2017 at 12:35
  • expected result json array @JinsPeter Commented Jun 6, 2017 at 12:36
  • one two three sometime four five also come @Nina Scholz Commented Jun 6, 2017 at 12:36
  • @jose can u show examle result.? Commented Jun 6, 2017 at 12:36

3 Answers 3

3

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; }

Sign up to request clarification or add additional context in comments.

3 Comments

it contains the keys for the static values. as hash key, it uses then value of the object with the key of index zero.
does it mean, you need to use both as grouping key?
Now it's working add two more fields in group by means what should i fo @Nina var key = a[staticKeys[0,1]];?
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.

1 Comment

try to make a fiddle @fxlacroix
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.