Home > database >  Object from array using deep copy
Object from array using deep copy

Time:04-25

I am trying to produce the below mentioned object from the array. I have tried map and push from Javascript but I could not achieve it. Please provide some glue to perform this.

fn();

function fn() {
  const env = 'uat';
  const userInput = 'appA';
  var listOfApplications = [{
      "appName": "common",
      "data": {
        "uat": {
          "baseUrl": "http://commonurl.com",
          "parameterX": "x",
          "parameterY": "y"
        }
      }
    },
    {
      "appName": "appA",
      "data": {
        "uat": {
          "baseUrl": "http://uat.appA.com",
          "parameterA": "a",
          "parameterB": "b"
        }
      }
    }
  ];

  var config = {};
  for (var i = 0; i < listOfApplications.length; i  ) {
    if (userInput == listOfApplications[i].appName) {
      config[userInput] === listOfApplications[i].data[env];
    } else if ('common' == listOfApplications[i].appName) {
      config === listOfApplications[i].data[env];
    }
  }

  console.log(config);
  return config;
}

I am trying to produce the below mentioned output.

 {
  "baseUrl": "http://commonurl.com",
  "parameterX": "x",
  "parameterY": "y",
  "appA": {
    "baseUrl": "http://uat.appA.com",
    "parameterA": "a",
    "parameterB": "b"
   }
 }

Please help me on this

CodePudding user response:

You're almost there. You just need to assign values instead of comparing values

But one thing I'd like to note down, your code has a small order problem. If common comes after appA, it will override your original values. You can check the code snippet below to represent that possible issue.

fn();

function fn() {
  const env = 'uat';
  const userInput = 'appA';
  var listOfApplications = [{
      "appName": "appA",
      "data": {
        "uat": {
          "baseUrl": "http://uat.appA.com",
          "parameterA": "a",
          "parameterB": "b"
        }
      }
    },
    {
      "appName": "common",
      "data": {
        "uat": {
          "baseUrl": "http://commonurl.com",
          "parameterX": "x",
          "parameterY": "y"
        }
      }
    }
  ];

  var config = {};
  for (var i = 0; i < listOfApplications.length; i  ) {
    if (userInput === listOfApplications[i].appName) {
      config[userInput] = listOfApplications[i].data[env];
    }
    if ('common' === listOfApplications[i].appName) {
      //THIS CODE IS OVERRIDING YOUR CONFIG VALUES
      config = listOfApplications[i].data[env];
    }
  }

  console.log(config);
  return config;
}

Here is the full working version and I'd suggest you use the spreading operation to clone your objects

fn();

function fn() {
  const env = 'uat';
  const userInput = 'appA';
  var listOfApplications = [{
      "appName": "common",
      "data": {
        "uat": {
          "baseUrl": "http://commonurl.com",
          "parameterX": "x",
          "parameterY": "y"
        }
      }
    },
    {
      "appName": "appA",
      "data": {
        "uat": {
          "baseUrl": "http://uat.appA.com",
          "parameterA": "a",
          "parameterB": "b"
        }
      }
    }
  ];

  var config = {};
  for (var i = 0; i < listOfApplications.length; i  ) {
    if (userInput === listOfApplications[i].appName) {
      //assign values to config
      config[userInput] = {...listOfApplications[i].data[env]};
    } 
    if ('common' === listOfApplications[i].appName) {
      //expand `config` instead of overriding values
      config = {...config, ...listOfApplications[i].data[env]};
    }
  }

  console.log(config);
  return config;
}

Shorter version with find

fn();

function fn() {
  const env = 'uat';
  const userInput = 'appA';
  var listOfApplications = [{
      "appName": "common",
      "data": {
        "uat": {
          "baseUrl": "http://commonurl.com",
          "parameterX": "x",
          "parameterY": "y"
        }
      }
    },
    {
      "appName": "appA",
      "data": {
        "uat": {
          "baseUrl": "http://uat.appA.com",
          "parameterA": "a",
          "parameterB": "b"
        }
      }
    }
  ];

  const common = listOfApplications.find(app => app.appName === "common").data[env]
  const environment = listOfApplications.find(app => app.appName === userInput).data[env]

  const config = {
    ...common,
    [userInput]: {
      ...environment
    }
  }

  console.log(config);
  return config;
}

CodePudding user response:

You can do:

(() => {
  const env = 'uat'
  const userInput = 'appA'
  const listOfApplications = [{appName: 'common',data: {uat: {baseUrl: 'http://commonurl.com',parameterX: 'x',parameterY: 'y'}}},{appName: 'appA',data: {uat: {baseUrl: 'http://uat.appA.com',parameterA: 'a',parameterB: 'b'}}}]
  const config = listOfApplications.reduce((a, { appName, data }) => (
    {
      [appName]: () => a[userInput] = data[env],
      common: () => a = { ...a, ...data[env] },
    }[appName](),
    a
  ), {})

  console.log(config)
  return config
})()

CodePudding user response:

Just change the strictly equal operator === to the assignment operator =

var config = {};
  for (var i = 0; i < listOfApplications.length; i  ) {
    if (userInput == listOfApplications[i].appName) {
      // here changed === to =
      config[userInput] = listOfApplications[i].data[env];
    } else if ('common' == listOfApplications[i].appName) {
      // here changed === to =      
      config =listOfApplications[i].data[env];
    }
  }
  • Related