2016-07-30 5 views
2

나는이 데이터를 기반으로 피벗 배열을 만들려고하고 있어요 안녕하세요에 배열을 변환하는방법 피벗 배열 JS

var data=[{model_num : "ABC", revision : "AA", "Pressure":31.25, "Gas":31.25, "Temp": 33.12,"Current":25.87 },{model_num : "ABC", revision : "AB", "Gas":26.00,"Temp":23.75}] 
내가 얻을 내 arr2d2를 기록 콘솔 때

var arr2d2 = []; //new array that is going to contain the merged values. 
    // console.log(arr2d2.length); 
    var arr2d2test = []; 
    var counter=0; 
    data.map(function(element){ 
     //console.log(counter); // this will run 7 times the size of data 
     var outerElement = element; //element is equivalent to one array for example: {model_num : "ABC", revision : "AA", value : 31.25, treatment : 'Pressure' } all 7 in this example 
     //console.log(element); 
    var found = false; //set initially to false. If not found add element to the new array. 
    //console.log(arr2d2.length); 
    for (var i = 0; i < arr2d2.length; i++) 
    { 
     //console.log(i); 
     //console.log(arr2d2[i].model_num); 
     if (arr2d2[i].model_num == outerElement.model_num && arr2d2[i].revision == outerElement.revision) 
     { 
      arr2d2[i][outerElement['treatment']] = outerElement['value']; 
      found = arr2d2[i]; // save the element. 
     // console.log('found variable' + JSON.stringify(found)); 

      break; //stop the loop 
     } 
    }; 

    if (found) 
    { 
     //arr2d2test.push(found.model_num);//   console.log(found.model_num); 
     if (found.value != outerElement.value) 
     { 
     // console.log('TRUE'); 
      found.value.push(outerElement.value); //push the age to the new value. 
     } 
    } 
    else 
    { 
     outerElement.value = [outerElement.value]; //convert age to an array, like you specified. 
    // console.log(outerElement); 
     arr2d2.push(outerElement); //not found yet. push element; 
    } 
counter++; 
}); 
//console.log(arr2d2test); 
console.log(arr2d2); 
console.log(JSON.stringify(arr2d2)); 

다음 :

나는 다음과 같은 코드가

[{"model_num":"ABC","revision":"AA","value":[31.25,33.12,25.87],"treatment":"Pressure","Gas":31.25,"Temp":33.12,"Current":25.87},{"model_num":"ABC","revision":"AB","value":[26.63,26,23.75],"treatment":"Pressure","Gas":26,"Temp":23.75}] 

정확하게 닫히지 만 필요한 것은 아닙니다. 어떤 도움이라도 대단히 감사하겠습니다.

답변

2

forEach 루프와 선택적 thisArg 매개 변수를 사용하여 원하는 결과를 얻을 수 있습니다. 내가 :) 필요 정확히 한

var data = [ 
 
    {model_num : "ABC", revision : "AA", value : 31.25, treatment : 'Pressure' }, 
 
    {model_num : "ABC", revision : "AA", value : 31.25, treatment : 'Gas' }, 
 
    {model_num : "ABC", revision : "AA", value : 33.12, treatment : 'Temp' }, 
 
    {model_num : "ABC", revision : "AA", value : 25.87, treatment : 'Current' }, 
 
    {model_num : "ABC", revision : "AB", value : 26.63, treatment : 'Pressure' }, 
 
    {model_num : "ABC", revision : "AB", value : 26.00, treatment : 'Gas' }, 
 
    {model_num : "ABC", revision : "AB", value : 23.75, treatment : 'Temp' } 
 
]; 
 

 

 
var result = []; 
 
data.forEach(function(e) { 
 
    var a = e.model_num + '|' + e.revision; 
 
    if(!this[a]) { 
 
    this[a] = {model_num: e.model_num, revision: e.revision} 
 
    result.push(this[a]); 
 
    } 
 
    this[a][e.treatment] = e.value; 
 
}, {}); 
 

 
document.body.innerHTML = '<pre>' + JSON.stringify(result, 0, 4) + '</pre>';

+0

감사 @Nenad! – cocopan