'Restructuring the object using different name [Object manipulation]

I have a use case where I have to restructure the object. Initial I have this object

{ 
    name: 'Tommy Kerar',
    mandatoryRequirements : [{
          name: "dummy.pdf",
          relativeUrl: "b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D",
        },
        {
          name: "Sell the Solution, Not the Product.png",
          relativeUrl:
            "6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D",
        },
        {
          name: "ales-nesetril-Im7lZjxeLhg-unsplash.jpg",
          relativeUrl:
            "2e07b606-d1a0-4587-b8bb-c8f4b3927483ales-nesetril-Im7lZjxeLhg-unsplash.jpg",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/2e07b606-d1a0-4587-b8bb-c8f4b3927483ales-nesetril-Im7lZjxeLhg-unsplash.jpg?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=HjMt%2Fpo1xc%2Bzt%2BeWaN%2FpU3vpY4A%3D",
        }],
    postGrad: [
        {
          name: "dummy.pdf",
          relativeUrl: "b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D",
        },
        {
          name: "Sell the Solution, Not the Product.png",
          relativeUrl:
            "6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D",
        }
    ],
    yourDocuments: [
         {
          name: "dummy.pdf",
          relativeUrl: "b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D",
        },
        {
          name: "Sell the Solution, Not the Product.png",
          relativeUrl:
            "6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D",
        }
    ]
}

As a result output I want something like this Major changes are I want to make one single array out form above object, with name as username, And folder names as per the above object keys with names in the below expected output.

PS: I am new to JS world. Previously with Python.

  const dd = { 
    name: 'Tommy Kerar',
    mandatoryRequirements : [{
          name: "dummy.pdf",
          relativeUrl: "b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D",
        },
        {
          name: "Sell the Solution, Not the Product.png",
          relativeUrl:
            "6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D",
        },
        {
          name: "ales-nesetril-Im7lZjxeLhg-unsplash.jpg",
          relativeUrl:
            "2e07b606-d1a0-4587-b8bb-c8f4b3927483ales-nesetril-Im7lZjxeLhg-unsplash.jpg",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/2e07b606-d1a0-4587-b8bb-c8f4b3927483ales-nesetril-Im7lZjxeLhg-unsplash.jpg?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=HjMt%2Fpo1xc%2Bzt%2BeWaN%2FpU3vpY4A%3D",
        }],
    postGrad: [
        {
          name: "dummy.pdf",
          relativeUrl: "b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D",
        },
        {
          name: "Sell the Solution, Not the Product.png",
          relativeUrl:
            "6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D",
        }
    ],
    yourDocuments: [
         {
          name: "dummy.pdf",
          relativeUrl: "b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D",
        },
        {
          name: "Sell the Solution, Not the Product.png",
          relativeUrl:
            "6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",
          url:
            "https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D",
        }
    ]
}

This is what I tried, But stuck at adding username and folder adding to the array list.

let newA = [];

newA = dd.postGrad
newA = dd.yourDocuments
newA = dd.mandatoryRequirements


console.log(newA)


Solution 1:[1]

You can use Object.entries, Array.isArray, flat & map..

I've done an example here with comments for each part.

eg.

var data={name:"Tommy Kerar",mandatoryRequirements:[{name:"dummy.pdf",relativeUrl:"b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D"},{name:"Sell the Solution, Not the Product.png",relativeUrl:"6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D"},{name:"ales-nesetril-Im7lZjxeLhg-unsplash.jpg",relativeUrl:"2e07b606-d1a0-4587-b8bb-c8f4b3927483ales-nesetril-Im7lZjxeLhg-unsplash.jpg",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/2e07b606-d1a0-4587-b8bb-c8f4b3927483ales-nesetril-Im7lZjxeLhg-unsplash.jpg?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=HjMt%2Fpo1xc%2Bzt%2BeWaN%2FpU3vpY4A%3D"}],postGrad:[{name:"dummy.pdf",relativeUrl:"b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D"},{name:"Sell the Solution, Not the Product.png",relativeUrl:"6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D"}],yourDocuments:[{name:"dummy.pdf",relativeUrl:"b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D"},{name:"Sell the Solution, Not the Product.png",relativeUrl:"6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D"}]};

console.log(
  Object.entries(data)  //get entries  key,value
    .filter(([_,v]) => Array.isArray(v))  //filter out values that are not arrays
    .map(([k,v]) =>   //use this map to do outer loop
      v.map(v2 => ({username: data.name, 
        foldername: 
          k.replace(/([a-z])([A-Z])/g, '$1 $2') //ex. split postGrad to `post Grad`
          .replace(/^./, v => v.toUpperCase()),  //now uppercase that first letter `Post Grad`
        ...v2}))) //map our sub arrays
    .flat()  //finally flattern
);

For new users to Javascript, the above code might look daunting. But it's not as bad as it looks, especially if we break it down. So below I'm also doing another version that's longer but should be much easier for new comers to JS to understand.

I've commented out some console.log's, you can un-comment to see each stage. But basically the code is doing exactly the same as the first snippet.

var data={name:"Tommy Kerar",mandatoryRequirements:[{name:"dummy.pdf",relativeUrl:"b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D"},{name:"Sell the Solution, Not the Product.png",relativeUrl:"6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D"},{name:"ales-nesetril-Im7lZjxeLhg-unsplash.jpg",relativeUrl:"2e07b606-d1a0-4587-b8bb-c8f4b3927483ales-nesetril-Im7lZjxeLhg-unsplash.jpg",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/2e07b606-d1a0-4587-b8bb-c8f4b3927483ales-nesetril-Im7lZjxeLhg-unsplash.jpg?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=HjMt%2Fpo1xc%2Bzt%2BeWaN%2FpU3vpY4A%3D"}],postGrad:[{name:"dummy.pdf",relativeUrl:"b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D"},{name:"Sell the Solution, Not the Product.png",relativeUrl:"6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D"}],yourDocuments:[{name:"dummy.pdf",relativeUrl:"b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/b9dde810-4935-4471-a22d-bdaf61e8bd62dummy.pdf?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=jefI4SMA0LlVutoBDkVZdDchSSY%3D"},{name:"Sell the Solution, Not the Product.png",relativeUrl:"6397c507-0d05-4cd7-8e4b-371249f72dbeSell the Solution, Not the Product..png",url:"https://upaged-file-store-prod.s3.ap-southeast-2.amazonaws.com/6397c507-0d05-4cd7-8e4b-371249f72dbeSell%20the%20Solution%2C%20Not%20the%20Product..png?AWSAccessKeyId=AKIAVRO4YTL2YDUPVPPW&Expires=1625593013&Signature=96fCwM%2FOz%2F60nt%2FCdhkdbM%2B04oo%3D"}]};


//lets get our data into the form of key value pairs.
//eg.  [
//  ["name", "Tommoy Kerar"],
//  ["mandatoryRequirements", [........]
//]
const keyValues = Object.entries(data);
//console.log(keyValues);

//next lets filter out array items who's value is not an array
//ps.  second array element [1] of each keyvalue is the value
const keyValuesArrayOnly = keyValues.filter(a => Array.isArray(a[1]));
//console.log(keyValuesArrayOnly);

//a simple function to convert camel case to a kind of Title
function camelCaseToTitle(txt) {
  return txt.replace(/([a-z])([A-Z])/g, '$1 $2') //ex. split postGrad to `post Grad`
    .replace(/^./, v => v.toUpperCase()); //uppercase first letter  `Post Grad`
}
//console.log(camelCaseToTitle('postGrad'));

//let's now map our values, here we use a map inside a map to copy the values across
//the reason we can't do later after flatterning is that we will loose the foldername
//links otherwise.  By doing a map inside a map, you then have the foldername scoped 
//for the inner array.
const mappedValues = keyValuesArrayOnly.
  map(([foldername, items]) => 
    items.map(value => ({
      username: data.name,
      foldername: camelCaseToTitle(foldername),
      ...value  //spread all the values on this new object.
    }))
  );
//console.log(mappedValues);

//finally let's flatten our results, as it's currently in form [[item,item], [item,item]]
//and we want [item, item, item, item]

const result = mappedValues.flat();
console.log(result);

Solution 2:[2]

The best thing that comes to my mind is to add the elements you have on each array to a new one with the desired properties set. I am not sure if any function call exists to make this process easier, I believe you will have to use forEach method on the arrays.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s
Solution 2 Manuel Gonzalez Garcia