Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda