Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

68
Views
Array objects to simple object mapping issue

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() {
  var env = 'uat';
  var 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"
         }
      }
   },
   {
      "appName":"appB",
      "data":{
         "uat":{
            "baseUrl":"http://uat.appB.com",
            "parameterC":"d",
            "parameterD":"c"
         }
      }
   }
];
  var config = {};
  switch (env) {
    case "uat":
        for (const s of listOfApplications) {
            if (userInput == s.appName || 'common' == s.appName) {
                config[s.appName] = s.appName
                config = s.data['uat']
            }
        }
     break;
  }
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

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

Problem:

First, I want to explain the problems in your code:

The line

config[s.appName] = s.appName

sets a property in config but the very next line

config = s.data['uat']

reassigns a new object and overwrites all previous changes. Doing this in a loop results in config containing s.data['uat'] of the last loop iteration where

userInput == s.appName || 'common' == s.appName

returns true. That means config either contains the common config or the app config. It also unintentionally modifies your original data. After the first loop iteration, config = s.data['uat'] sets a reference to an object in your original object. Then, config = s.data['uat'] adds a property to this object. You can see the problem with a console.log:

fn();

function fn() {
  var env = 'uat';
  var 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"}}},{"appName": "appB","data": {"uat": {"baseUrl": "http://uat.appB.com","parameterC": "d","parameterD": "c"}}}];
  var config = {};
  switch (env) {
    case "uat":
      for (const s of listOfApplications) {
        if (userInput == s.appName || 'common' == s.appName) {
          config[s.appName] = s.appName
          config = s.data['uat']
        }
      }
      break;
  }
  console.log(listOfApplications);
  return config;
}

The first array element in listOfApplications unintentionally got a new property.

Solution:

It looks like the expected result is an object containing all properties of the common configuration and a property with app configuration. You can find both configurations using Array#find:

fn();

function fn() {
  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"}}},{"appName": "appB","data": {"uat": {"baseUrl": "http://uat.appB.com","parameterC": "d","parameterD": "c"}}}];
  const config = {
    ...listOfApplications.find(({ appName }) => appName === 'common')?.data[env],
    [userInput]: { ...listOfApplications.find(({ appName }) => appName === userInput)?.data[env] }
  };
  console.log(config);
  return config;
}

I created a shallow copy of the app configuration to avoid unexpected modifications of the original object. For me complex configurations with nested objects, you should consider a deep copy.

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs