Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to transform object in array to create multiples of it's own if object value is an array

I have an array of objects which have the same structure except that some values have arrays as the type to signify that it has multiple values. I need to create a new object in that array based on how many items in the array.

const arrayOfObjects = [
  {
      "Serial Number": "83675327350061093222581609820467",
      "Part Number": "LJ98-10C779-AA",
      "Cell Count": "32",
      "Length": "6"
  },
  {
      "Serial Number": "19584757350061093222581606739916",
      "Type": "Extended Range",
      "Part Number": "SW23",
      "Length": "50"
  },
  {
      "Serial Number": [
          "90946117350061093222581604839920",
          "77362117350061093222581604891733",
          "77362117350061093222581604891734",
      ],
      "Part Number": [
          "TR40-60C779-AA",
          "ST41-60C780-AA",
          "ZT41-60C780-AA",
      ],
      "Cell Count": [
          "28",
          "27",
          "26"
      ],
      "Length": [
          "10",
          "11",
          "12"
      ]
  }
]

Below is how i want to transform it

const transformedArray = [
  {
      "Serial Number": "83675327350061093222581609820467",
      "Part Number": "LJ98-10C779-AA",
      "Cell Count": "32",
      "Length": "6"
  },
  {
      "Serial Number": "19584757350061093222581606739916",
      "Type": "Extended Range",
      "Part Number": "SW23",
      "Length": "50"
  },
   {
      "Serial Number": "90946117350061093222581604839920",
      "Part Number": "TR40-60C779-AA",
       "Cell Count": "28",
      "Length": "10"
  },
  {
      "Serial Number": "77362117350061093222581604891733",
      "Part Number": "ST41-60C780-AA",
      "Cell Count": "27",
      "Length": "11"
  },
  {
    "Serial Number": "77362117350061093222581604891734",
    "Part Number": "ZT41-60C780-AA",
    "Cell Count": "26",
    "Length": "12"
  },
]

I have tried to solve it with the below code attempt, please help complete:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let newItemsArray = [];

arrayOfObjects.map((item,index) => {
    return Object.entries(item).map(([key,value]) => {
        if(Array.isArray(value)){
            value.forEach(x => {
                newItemsArray.push({[key]:x})    
            })
            
        }
    })
})

>Solution :

You could check if the entries of the object contains an array and reduce the entries or take the object for mapping.

const
    data = [{ "Serial Number": "83675327350061093222581609820467", "Part Number": "LJ98-10C779-AA", "Cell Count": "32", "Length": "6" }, { "Serial Number": "19584757350061093222581606739916", "Type": "Extended Range", "Part Number": "SW23", "Length": "50" }, { "Serial Number": [ "90946117350061093222581604839920", "77362117350061093222581604891733", "77362117350061093222581604891734"], "Part Number": [ "TR40-60C779-AA", "ST41-60C780-AA", "ZT41-60C780-AA"], "Cell Count": [ "28", "27", "26"], "Length": [ "10", "11", "12"] }],
    result = data.flatMap(object => {
        const entries = Object.entries(object);
        return Array.isArray(entries[0][1])
            ? entries.reduce((r, [k, a]) => a.map((v, i) => ({ ...r[i], [k]: v })), [])
            : object;
    });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Without flatMap

const
    data = [{ "Serial Number": "83675327350061093222581609820467", "Part Number": "LJ98-10C779-AA", "Cell Count": "32", "Length": "6" }, { "Serial Number": "19584757350061093222581606739916", "Type": "Extended Range", "Part Number": "SW23", "Length": "50" }, { "Serial Number": [ "90946117350061093222581604839920", "77362117350061093222581604891733", "77362117350061093222581604891734"], "Part Number": [ "TR40-60C779-AA", "ST41-60C780-AA", "ZT41-60C780-AA"], "Cell Count": [ "28", "27", "26"], "Length": [ "10", "11", "12"] }],
    result = data.reduce((array, object) => {
        const entries = Object.entries(object);
        if (Array.isArray(entries[0][1])) return [
            ...array,
            entries.reduce((r, [k, a]) => a.map((v, i) => ({ ...r[i], [k]: v })), [])
        ];
        return [...array, object];
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading