简体   繁体   中英

Sort javascript objects and combine objects into an array

I have the following implementation and its functional

https://jsfiddle.net/9mv6w0da/

Here is implementation:

function groupBy(arr, f) {
  var result = {};
  arr.forEach(function(elem) {
    var fElem = f(elem),
        list = result[fElem] || [];
    list.push(elem);
    result[fElem] = list;
  });
  return result;
}

function objToArray(obj) {
  var result = [];
  for(k in obj) {
    result.push(obj[k]);
  }
  return result;
}


var allData = dataSet.reduce(function(a, b) { return a.concat(b) });
var grouped = objToArray(groupBy(allData, function(data) { return data.color + "#" + data.weight }));

console.log(grouped);

Input

dataSet[0]= [
    {color:"yellow",weight:12}
   ,{color:"yellow",weight:13}
   ,{color:"yellow",weight:11}
   ,{color:"red",weight:15}
   ,{color:"red",weight:18}
];          
dataSet[1]= [
    {color:"yellow",weight:22}
   ,{color:"yellow",weight:32}
   ,{color:"red",weight:3}
   ,{color:"red",weight:9}
   ,{color:"blue",weight:10}
   ,{color:"blue",weight:8}
];

Current Output - grouped based on color

an[0]=[
    {color:"yellow",weight:12}
   ,{color:"yellow",weight:13}
   ,{color:"yellow",weight:11}
   ,{color:"yellow",weight:22}
   ,{color:"yellow",weight:32}
]
an[1]=[
    {color:"red",weight:15}
   ,{color:"red",weight:18}
   ,{color:"red",weight:3}
   ,{color:"red",weight:9}
]
an[2]=[{color:"blue",weight:10},{color:"blue",weight:8}]

However, I would like to have the following output to put weight values in an array rather than having a multiple javascript objects.

Desired Output

an[0]=[{color:"yellow",weight:[12,13,11,22,32]}]
an[1]=[{color:"red",weight:[15,18,3,9]}]
an[2]=[{color:"blue",weight:[10,8]}]
var dataSet = [
    [
        {color:"yellow",weight:12}
       ,{color:"yellow",weight:13}
       ,{color:"yellow",weight:11}
       ,{color:"red",weight:15}
       ,{color:"red",weight:18}
    ],          
    [
        {color:"yellow",weight:22}
       ,{color:"yellow",weight:32}
       ,{color:"red",weight:3}
       ,{color:"red",weight:9}
       ,{color:"blue",weight:10}
       ,{color:"blue",weight:8}
    ]
];

function indexByColor (input) {
    var output = {};
    for (var i in input) {
        for (var j in input[i]) {
            var x = input[i][j];
            if (output[x.color] === undefined) output[x.color] = {
                color: x.color, 
                weight: [],
            };  
            output[x.color].weight.push(x.weight);
        };  
    };  
    return Object.keys(output).map(function(c){
            return output[c];
    });     
};  

console.log (indexByColor(dataSet));

Here is the jsfiddle: https://jsfiddle.net/f3syta4f/

Try this:

<script>
    var dataSet=[];
    dataSet[0]=[
        {color: "yellow", weight: 12}
        , {color: "yellow", weight: 13}
        , {color: "yellow", weight: 11}
        , {color: "red", weight: 15}
        , {color: "red", weight: 18}
    ];
    dataSet[1]=[
        {color: "yellow", weight: 22}
        , {color: "yellow", weight: 32}
        , {color: "red", weight: 3}
        , {color: "red", weight: 9}
        , {color: "blue", weight: 10}
        , {color: "blue", weight: 8}
    ];
    var colorArr=[];
    var finalArr=[];
    for(var i=0; i < dataSet.length; i++)
    {
        (function(j)
        {
            for(var i=0; i < dataSet[j].length; i++)
            {
                if(colorArr.indexOf(dataSet[j][i]['color']) != -1)
                {
                    var index=getIndexByValue(finalArr, 'color', dataSet[j][i]['color']);
                    finalArr[index]['weight'].push(dataSet[j][i]['weight']);
                }
                else
                {
                    finalArr.push({'color': dataSet[j][i]['color']});
                    colorArr.push(dataSet[j][i]['color']);
                    var index=getIndexByValue(finalArr, 'color', dataSet[j][i]['color']);
                    finalArr[index]['weight']=[dataSet[j][i]['weight']];
                }
            }
        })(i);
    }
    function getIndexByValue(array, key, value)
    {
        for(var i=0; i < array.length; i++)
        {
            if(array[i][key] === value)
            {
                return i;
            }
        }
    }
    console.log(finalArr);
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM