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}]
정확하게 닫히지 만 필요한 것은 아닙니다. 어떤 도움이라도 대단히 감사하겠습니다.
감사 @Nenad! – cocopan