Skip to content Skip to sidebar Skip to footer

Grouping An Array And Counting Items Creating New Array Based On Groups

My knowledge on manipulating arrays is really poor, i was wandering if i could get any help with this. i have an array('dataResult') .that has got Region, Fruits and User informati

Solution 1:

With proper data, you could iterate the array of objects and count the regions.

var dataResult = [{ region: "Africa", fruit: "Orange", user: "Gary" }, { region: "Africa", fruit: "Apple", user: "Steve" }, { region: "Europe", fruit: "Orange", user: "John" }, { region: "Europe", fruit: "Apple", user: "bob" }, { region: "Asia", fruit: "Orange", user: "Ian" }, { region: "Asia", fruit: "Apple", user: "Angelo" }, { region: "Africa", fruit: "Orange", user: "Gary" }],
    grouped = [];

dataResult.forEach(function (a) {
    if (!this[a.region]) {
        this[a.region] = { region: a.region, count: 0 };
        grouped.push(this[a.region]);
    }
    this[a.region].count++;
}, Object.create(null));
  
console.log(grouped);
.as-console-wrapper { max-height: 100%!important; top: 0; }

For unique user, you might use an extended hash table

var dataResult = [{ region: "Africa", fruit: "Orange", user: "Gary" }, { region: "Africa", fruit: "Apple", user: "Steve" }, { region: "Europe", fruit: "Orange", user: "John" }, { region: "Europe", fruit: "Apple", user: "bob" }, { region: "Asia", fruit: "Orange", user: "Ian" }, { region: "Asia", fruit: "Apple", user: "Angelo" }, { region: "Africa", fruit: "Orange", user: "Gary" }],
    grouped = [];

dataResult.forEach(function (a) {
    var key = [a.region, a.user].join('|');
    if (!this[a.region]) {
        this[a.region] = { region: a.region, count: 0 };
        grouped.push(this[a.region]);
    }
    if (!this[key]) {
        this[key] = true;
        this[a.region].count++;
    }
}, Object.create(null));
  
console.log(grouped);
.as-console-wrapper { max-height: 100%!important; top: 0; }

Part with a decent look at the hash table and some explanations in the code.

var dataResult = [{ region: "Africa", fruit: "Orange", user: "Gary" }, { region: "Africa", fruit: "Apple", user: "Steve" }, { region: "Europe", fruit: "Orange", user: "John" }, { region: "Europe", fruit: "Apple", user: "bob" }, { region: "Asia", fruit: "Orange", user: "Ian" }, { region: "Asia", fruit: "Apple", user: "Angelo" }, { region: "Africa", fruit: "Orange", user: "Gary" }],
    grouped = [],               // result array
    hash = Object.create(null); // oposite of {}, this object does not contain any prototypes

dataResult.forEach(function (a) {

    // build key with region + name, spot pipe in hashvar key = [a.region, a.user].join('|');

    // check if region is not in hash tableif (!this[a.region]) {

        // create new object with region and count and insert it into hash tablethis[a.region] = { region: a.region, count: 0 };

        // push the object to the result
        grouped.push(this[a.region]);
    }

    // check if if not region and user exists, it means// the user in the region is not counted yet.if (!this[key]) {

        //create hash entrythis[key] = true;

        // increment count this[a.region].count++;
    }
}, hash);
  
console.log(hash);
console.log(grouped);
.as-console-wrapper { max-height: 100%!important; top: 0; }

Solution 2:

What you want to do is turn your array into an object because your final array is not a valid array.

So you want to return something that looks like this.

newResults= {Africa:2, Europe:2, Asia:2}

You would need to right a function that iterates through the array and returns a new object with the desired key and value pairs. Take a look at the map and reduce functions in Javascript, they may be some help.

Reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Post a Comment for "Grouping An Array And Counting Items Creating New Array Based On Groups"