对javascript对象进行排序并将对象组合成一个数组

时间:2015-05-28 16:42:56

标签: javascript

我有以下实现及其功能

https://jsfiddle.net/9mv6w0da/

这是实施:

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

输入

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

当前输出 - 根据颜色分组

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

但是,我希望有以下输出将权重值放在数组中,而不是拥有多个javascript对象。

所需输出

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

2 个答案:

答案 0 :(得分:3)

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

这是jsfiddle:https://jsfiddle.net/f3syta4f/

答案 1 :(得分:1)

试试这个:

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