数组对象操作以创建新对象

时间:2015-07-22 15:00:42

标签: javascript

var actual = [
 {"country":"UK","month":"JAN","SR":"John P","AC":"24","PR":"2","TR":1240},
 {"country":"AUSTRIA","month":"JAN","SR":"Brad    P","AC":"64","PR":"12","TR":1700},
 {"country":"ITALY","month":"JAN","SR":"Gim P","AC":"21","PR":"5","TR":900},

 {"country":"UK","month":"FEB","SR":"John P","AC":"14","PR":"4","TR":540},
 {"country":"AUSTRIA","month":"FEB","SR":"Brad P","AC":"24","PR":"12","TR":1700},
 {"country":"ITALY","month":"FEB","SR":"Gim P","AC":"22","PR":"3","TR":600},

 {"country":"UK","month":"MAR","SR":"John P","AC":"56","PR":"2","TR":1440},
 {"country":"AUSTRIA","month":"MAR","SR":"Brad P","AC":"24","PR":"12","TR":700},
 {"country":"ITALY","month":"MAR","SR":"Gim P","AC":"51","PR":"5","TR":200}
 ];

var expect = [
{month:"JAN",val: {"UK":"24","AUSTRIA":"64","ITALY":"21"}},
{month:"FEB",val: {"UK":"14","AUSTRIA":"24","ITALY":"22"}},
{month:"MAR",val: {"UK":"56","AUSTRIA":"24","ITALY":"51"}}
];

我有一些对象需要重塑另一个工作。需要一些可以通过一个函数转换的操作。我创建了plunker https://jsbin.com/himawakaju/edit?html,js,console,output

主要因素是月,国家及其" AC"值。

3 个答案:

答案 0 :(得分:1)

遍历数组并创建新列表



var actual = [
 {"country":"UK","month":"JAN","SR":"John P","AC":"24","PR":"2","TR":1240},
 {"country":"AUSTRIA","month":"JAN","SR":"Brad    P","AC":"64","PR":"12","TR":1700},
 {"country":"ITALY","month":"JAN","SR":"Gim P","AC":"21","PR":"5","TR":900},

 {"country":"UK","month":"FEB","SR":"John P","AC":"14","PR":"4","TR":540},
 {"country":"AUSTRIA","month":"FEB","SR":"Brad P","AC":"24","PR":"12","TR":1700},
 {"country":"ITALY","month":"FEB","SR":"Gim P","AC":"22","PR":"3","TR":600},

 {"country":"UK","month":"MAR","SR":"John P","AC":"56","PR":"2","TR":1440},
 {"country":"AUSTRIA","month":"MAR","SR":"Brad P","AC":"24","PR":"12","TR":700},
 {"country":"ITALY","month":"MAR","SR":"Gim P","AC":"51","PR":"5","TR":200}
 ];

var newList =[], val;
for(var i=0; i < actual.length; i+=3){
   val = {};
   val[actual[i].country] = actual[i]["AC"];
  val[actual[i+1].country] = actual[i+1]["AC"];
  val[actual[i+2].country] = actual[i+2]["AC"];
   newList.push({month: actual[i].month, val:val})
}

document.body.innerHTML = JSON.stringify(newList);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

循环,创建一个对象,然后循环创建数组

var actual = [
 {"country":"UK","month":"JAN","SR":"John P","AC":"24","PR":"2","TR":1240},
 {"country":"AUSTRIA","month":"JAN","SR":"Brad    P","AC":"64","PR":"12","TR":1700},
 {"country":"ITALY","month":"JAN","SR":"Gim P","AC":"21","PR":"5","TR":900},

 {"country":"UK","month":"FEB","SR":"John P","AC":"14","PR":"4","TR":540},
 {"country":"AUSTRIA","month":"FEB","SR":"Brad P","AC":"24","PR":"12","TR":1700},
 {"country":"ITALY","month":"FEB","SR":"Gim P","AC":"22","PR":"3","TR":600},

 {"country":"UK","month":"MAR","SR":"John P","AC":"56","PR":"2","TR":1440},
 {"country":"AUSTRIA","month":"MAR","SR":"Brad P","AC":"24","PR":"12","TR":700},
 {"country":"ITALY","month":"MAR","SR":"Gim P","AC":"51","PR":"5","TR":200}
 ];

var outTemp = {};
actual.forEach(function(obj){ //loop through array
    //see if we saw the month already, if not create it
    if(!outTemp[obj.month]) outTemp[obj.month] = { month : obj.month, val: {} };
    outTemp[obj.month].val[obj.country] = obj.AC;  //add the country with value
});
var expected = [];  //convert the object to the array format that was expected
for (var p in outTemp) {
    expected.push(outTemp[p]);
}
console.log(expected);

答案 2 :(得分:0)

这是正确的代码......如果有3行并且这些行将处于相同的序列中,上述解决方案将对您有所帮助。

这是完美的解决方案:

   var actual = [
                         {"country":"UK","month":"JAN","SR":"John P","AC":"24","PR":"2","TR":1240},
                         {"country":"AUSTRIA","month":"JAN","SR":"Brad    P","AC":"64","PR":"12","TR":1700},
                         {"country":"ITALY","month":"JAN","SR":"Gim P","AC":"21","PR":"5","TR":900},

                         {"country":"UK","month":"FEB","SR":"John P","AC":"14","PR":"4","TR":540},
                         {"country":"AUSTRIA","month":"FEB","SR":"Brad P","AC":"24","PR":"12","TR":1700},
                         {"country":"ITALY","month":"FEB","SR":"Gim P","AC":"22","PR":"3","TR":600},

                         {"country":"UK","month":"MAR","SR":"John P","AC":"56","PR":"2","TR":1440},
                         {"country":"AUSTRIA","month":"MAR","SR":"Brad P","AC":"24","PR":"12","TR":700},
                         {"country":"ITALY","month":"MAR","SR":"Gim P","AC":"51","PR":"5","TR":200}
                    ];
            var tmpArray = [];
            var obj =[];
            for(var k=0; k<actual.length; k++){

                var position = tmpArray.indexOf(actual[k].month);
                if(position == -1){
                    tmpArray.push(actual[k].month);
                    val = {};
                    for(var i=0; i<actual.length; i++){
                        if(actual[i].month == actual[k].month){
                            val[actual[i].country] = actual[i]["AC"];
                        }
                    }
                    obj.push({month: actual[k].month, val:val});
                }


            }