2

Hello I am trying to create a pivot array based on this data:

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

I would like the end result to look like this:

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

I have the following code:

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

When I console log my arr2d2 I get the following:

[{"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}]

which is close but not exactly what I need. Any help will be greatly appreciated.

1 Answer 1

2

You can use forEach loop and optional thisArg parameter to get desired result.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.