Answers for "group items of list by proprety value js"

0

group items of list by proprety value js

function groupBy(collection, property) {
    var i = 0, val, index,
        values = [], result = [];
    for (; i < collection.length; i++) {
        val = collection[i][property];
        index = values.indexOf(val);
        if (index > -1)
            result[index].push(collection[i]);
        else {
            values.push(val);
            result.push([collection[i]]);
        }
    }
    return result;
}

var obj = groupBy(list, "group");


//https://stackoverflow.com/questions/21776389/javascript-object-grouping
Posted by: Guest on June-28-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language