Home > Back-end >  Split Array Of Object with JavaScript
Split Array Of Object with JavaScript

Time:08-17

I have the following array of objects, which I want to split / seperate: I know how to do it if I have only one object but I have trouble spliting it within an array of objects. The array of objects looks like the following. See down below:

[
    {
        "KPI": "Productivty [%] - Target",
        "Area": "Plant",
        "Jan22": 0.8621397507374327,
        "Feb22": 0.8605700219733681,
        "Mrz22": 0.8870898346258058
        
      },
      {
        "KPI": "Productivty [%] - Target",
        "Area": "Assembly",
        "Jan22": 0.8817383050990651,
        "Feb22": 0.8856950523200521,
        "Mrz22": 0.9267495852228734
        
      },
      {
        "KPI": "Productivty [%] - Target",
        "Area": "Assembly CYU",
        "Jan22": 0.8984342053383161,
        "Feb22": 0.9285678969440421,
        "Mrz22": 0.9625283644615115
       
      }
]

I want to split each object so it looks like the following : I know

[
    {
        "KPI": "Productivty [%] - Target 0 ",
        "Area": "Plant",
        "Month": 0.8621397507374327
      },
      {
        "KPI": "Productivty [%] - Target 1 ",
        "Area": "Plant",
        "Month": 0.8605700219733681
      },
      {
        "KPI": "Productivty [%] - Target 2 ",
        "Area": "Plant",
        "Month": 0.8870898346258058
      }
      
      {
        "KPI": "Productivty [%] - Target 0 ",
        "Area": "Assembly",
        "Month": 0.8817383050990651
      },
      {
        "KPI": "Productivty [%] - Target 1 ",
        "Area": "Assembly",
        "Month": 0.8856950523200521
      },
      {
        "KPI": "Productivty [%] - Target 2 ",
        "Area": "Assembly",
        "Month": 0.9267495852228734
      }
      
      {
        "KPI": "Productivty [%] - Target 0 ",
        "Bereich": "Assembly CYU",
        "Month": 0.8984342053383161
      },
      {
        "KPI": "Productivty [%] - Target 1 ",
        "Area": "Assembly CYU",
        "Month": 0.9285678969440421
      },
      {
        "KPI": "Productivty [%] - Target 2 ",
        "Area": "Assembly CYU",
        "Month":0.9625283644615115
      }
]

I was trying to use the following :

Ausgabe2 = [];
 obj = 
        {
                "KPI": "Productivty [%] - Target",
                "Area": "Plant",
                "Jan22": 0.8621397507374327,
                "Feb22": 0.8605700219733681,
                "Mrz22": 0.8870898346258058
                
              },
              {
                "KPI": "Productivty [%] - Target",
                "Area": "Assembly",
                "Jan22": 0.8817383050990651,
                "Feb22": 0.8856950523200521,
                "Mrz22": 0.9267495852228734
                
              },
              {
                "KPI": "Productivty [%] - Target",
                "Area": "Assembly CYU",
                "Jan22": 0.8984342053383161,
                "Feb22": 0.9285678969440421,
                "Mrz22": 0.9625283644615115
               
              }
        
        
        
    const fn = ({ KPI, Area, ...rest }) =>
      Object.values(rest)
        .map(Month => ({
          KPI,
          Area,
          Month
        }))
    
    const result = fn(obj)
    
    for (var i =0; i < result.length; i  ){
      obj2 = {
        
        KPI : result[i].KPI   " "   i,
        Bereich: result[i].Area,
        Month : result[i].Month , 
         
        
      }
      Ausgabe2.push(obj2);
    }
       

   console.log(Ausgabe2);

But I do not get the desired output which I showed above. Instead I get this :

[ { KPI: 'Productivty [%] - Target 0',
    Bereich: 'Plant',
    Month: 0.8621397507374327 },
  { KPI: 'Productivty [%] - Target 1',
    Bereich: 'Plant',
    Month: 0.8605700219733681 },
  { KPI: 'Productivty [%] - Target 2',
    Bereich: 'Plant',
    Month: 0.8870898346258058 } ]

How do I get the desired output ?

CodePudding user response:

I'd suggest using Array.flatMap(), along with Object.values() and to create the desired result.

We'd create an entry for each month, adding the month index to the KPI string. The result will be flattened so should look as required.

const input = [ { "KPI": "Productivty [%] - Target", "Area": "Plant", "Jan22": 0.8621397507374327, "Feb22": 0.8605700219733681, "Mrz22": 0.8870898346258058  }, { "KPI": "Productivty [%] - Target", "Area": "Assembly", "Jan22": 0.8817383050990651, "Feb22": 0.8856950523200521, "Mrz22": 0.9267495852228734  }, { "KPI": "Productivty [%] - Target", "Area": "Assembly CYU", "Jan22": 0.8984342053383161, "Feb22": 0.9285678969440421, "Mrz22": 0.9625283644615115  } ]

const result = input.flatMap(({ KPI, Area, ...months }) => { 
    return Object.values(months).map((Month, idx) => {
        return { KPI: KPI   ` ${idx}`, Area, Month};
    })
})

console.log('Result:', result);
.as-console-wrapper { max-height: 100% !important; }

CodePudding user response:

From your description, it seems that the keys KPI and Area are common to all objects. You can simply iterate over the array and split each record in to the months it contains.

const records = [{
    "KPI": "Productivty [%] - Target",
    "Area": "Plant",
    "Jan22": 0.8621397507374327,
    "Feb22": 0.8605700219733681,
    "Mrz22": 0.8870898346258058

  },
  {
    "KPI": "Productivty [%] - Target",
    "Area": "Assembly",
    "Jan22": 0.8817383050990651,
    "Feb22": 0.8856950523200521,
    "Mrz22": 0.9267495852228734

  },
  {
    "KPI": "Productivty [%] - Target",
    "Area": "Assembly CYU",
    "Jan22": 0.8984342053383161,
    "Feb22": 0.9285678969440421,
    "Mrz22": 0.9625283644615115

  }
]

const months = new Set(["Jan22", "Feb22", "Mrz22"]);
const formattedRecords = [];

for (let record of records) {
  for (let key in record) {
    if (months.has(key)) {
      formattedRecords.push({
        "KPI": record["KPI"],
        "Area": record["Area"],
        [key]: record[key]
      });
    }
  }
}

console.log(formattedRecords)

CodePudding user response:

const arr = [
  {
    "KPI": "Productivty [%] - Target",
    "Area": "Plant",
    "Jan22": 0.8621397507374327,
    "Feb22": 0.8605700219733681,
    "Mrz22": 0.8870898346258058

  },
  {
    "KPI": "Productivty [%] - Target",
    "Area": "Assembly",
    "Jan22": 0.8817383050990651,
    "Feb22": 0.8856950523200521,
    "Mrz22": 0.9267495852228734

  },
  {
    "KPI": "Productivty [%] - Target",
    "Area": "Assembly CYU",
    "Jan22": 0.8984342053383161,
    "Feb22": 0.9285678969440421,
    "Mrz22": 0.9625283644615115

  }
]

const result = arr.map(({ KPI, Area, ...months }) => {
  return Object.values(months).map((Month) => {
    return { KPI, Area, Month }
  })
}).flat();
console.log(result)
  • Related