Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda