Home > database >  How to split an object within an array into multiple objects?
How to split an object within an array into multiple objects?

Time:08-26

I have an array of objects like so where, as in the name, fixedValue is fixed and both category and total are also fixed within each object in the inner fixedValue array. However "Col 2", "Col 3", etc. can be random values and there can be multiple of them. So, all other keys other than category or total could be named "FERFVCEEF erfe" or any random word:

originalDataSet = [
    {
        "fixedValue": [
            {
                "category": "SD",
                "total": 2,
                "Col 2": 208,
                "Col 3": 88,
                "Col 4": 4,
                ....
            },
            {
                "category": "EFG",
                "total": 1,
                "Col 2": 106,
                "Col 3": 46,
                "Col 4": 4,
                ....
            },
            {
                "category": "ERG",
                "total": 1,
                "Col 2": 107,
                "Col 3": 47,
                "Col 4": 5,
                ....
            },
            ....
        ]
    }
]

My goal is to convert it to the following:

newDataSet = [{
  fixedValue: [
    {
      name: "SD",
      fixedValue: [
        { name: "Col 2", value: 208 },
        { name: "Col 3", value: 88 },
        { name: "Col 4", value: 4 },
        ...
      ]
    },
    {
      name: "EFG",
      fixedValue: [
        { name: "Col 2", value: 106 },
        { name: "Col 3", value: 46 },
        { name: "Col 4", value: 4 },
        ...
      ]
    },
    {
      name: "ERG",
      fixedValue: [
        { name: "Col 2", value: 107 },
        { name: "Col 3", value: 47 },
        { name: "Col 4", value: 5 },
        ...
      ]
    },
    ....
  ]}
]

It takes each "category" value in the originalDataSet, and makes it the value of a new key called "name", with the key and key values that is not "category" or "total" becoming assigned to new keys.

I have something like this so far but I feel like it's going in the wrong direction:

// First work with the array in originalDataSet.fixedValue
const newObj = {};

// For each object in the fixedValue array
for (const key of originalDataSet.fixedValue) {

    // If the key does not equal to 'category' or 'total' e.g., in this example we're referring to 'Col 2', 'Col 3', and 'Col 4', though there could be more.
    if(!key['category'] && !key['total'])

        // Should yield something like this: { name: "Col 2", value: 107 }
        newObj['name']  = key;
        newObj['value'] = newObj[key];
    }
}

originalDataSet.fixedValue.foreach(
    newDataSet = [{
        fixedValue: [
            name: item.category
            fixedValue: [newObj]
        ]
    }]
}

How would I be able to achieve the newDataSet?

CodePudding user response:

You can use destructuring to isolate the category and total properties from the rest, and then use Object.entries on that rest object to map those key/value pairs to little {name, value} objects:

const convert = arr => arr.map(({fixedValue}) => ({ 
    fixedValue: fixedValue.map(({category, total, ...rest}) => ({
        name: category,
        fixedValue: Object.entries(rest).map(([name, value]) => ({ name, value }))
    }))
}));

const originalDataSet = [{"fixedValue": [{"category": "SD","total": 2,"Col 2": 208,"Col 3": 88,"Col 4": 4,}, {"category": "EFG","total": 1,"Col 2": 106,"Col 3": 46,"Col 4": 4,}, {"category": "ERG","total": 1,"Col 2": 107,"Col 3": 47,"Col 4": 5,}]}]

console.log(convert(originalDataSet))

CodePudding user response:

This data look like JSON you can easily convert it by the method :

const originalDataSet = {
"fixedValue": [{
        "category": "SD",
        "total": 2,
        "Col 2": 208,
        "Col 3": 88,
        "Col 4": 4
    },
    {
        "category": "EFG",
        "total": 1,
        "Col 2": 106,
        "Col 3": 46,
        "Col 4": 4
    },
    {
        "category": "ERG",
        "total": 1,
        "Col 2": 107,
        "Col 3": 47,
        "Col 4": 5
    }
]
}

const obj = JSON.stringify(originalDataSet);
const obj1 = JSON.parse(obj)
const newObj = obj1.fixedValue.map(el => {
  const newObject={
    name:el.category,
    fixedValue:[]
  }
  newObject.fixedValue.push({name:"Col 2", value:el["Col 2"]})
  newObject.fixedValue.push({name:"Col 3", value:el["Col 3"]})
  newObject.fixedValue.push({name:"Col 4", value:el["Col 4"]})
  return newObject
 }) 

Its possible to improve the code with the loop

  • Related