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

214
Visualizações
Convert Keys in an array to Lower case & remove spaces between keys if available (JS)

I need to convert Keys into lowercases and remove the spaces between them if any. All the keys are Dynamic and could be any number of keys. Keys might contain alphanumeric along with special characters.Javascript or Typescript anything would do for me. I'm little stuck here.

Current JSON :

[
    {
      "Outdated Label (Key1)": "Outdated Label1",
      "Outdated Value": "Outdated Value"
    },
    {
      "Outdated Label (Key1)": "Outdated Label2",
      "Outdated Value": "Outdated Value2"
    },
    {
      "Outdated Label (Key1)": "Outdated Label3",
      "Outdated Value": "Outdated Value3"
    },
    {
      "Outdated Label (Key1)": "Outdated Label4",
      "Outdated Value": "Outdated Value4"
    }
  ]

Expected JSON :

 [
    {
      "outdatedlabel(key1)": "Outdated Label1",
      "outdatedvalue": "Outdated Value"
    },
    {
      "outdatedlabel(key1)": "Outdated Label2",
      "outdatedvalue": "Outdated Value2"
    },
    {
      "outdatedlabel(key1)": "Outdated Label3",
      "outdatedvalue": "Outdated Value3"
    },
    {
      "outdatedlabel(key1)": "Outdated Label4",
      "outdatedvalue": "Outdated Value4"
    }
  ]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I have taken a look at the problem and came up with a solution that works and is written in plain JavaScript. This solution works for the given array and any other array of objects that are not nested. As you want to have a new array we can use .map and then iterate over every key in the current object and modify the key in such a way that it satisfies your requirements.

let o = [
  {
    "Outdated Label (Key1)": "Outdated Label1",
    "Outdated Value": "Outdated Value"
  },
  {
    "Outdated Label (Key1)": "Outdated Label2",
    "Outdated Value": "Outdated Value2"
  },
  {
    "Outdated Label (Key1)": "Outdated Label3",
    "Outdated Value": "Outdated Value3"
  },
  {
    "Outdated Label (Key1)": "Outdated Label4",
    "Outdated Value": "Outdated Value4"
  }
]

let whitespace = new RegExp(/\s/g);
let lo = o.map((entry) => {
  let modified = {};
  Object.keys(entry).forEach((key) => {
    let value = entry[key];
    key = key
      .toLowerCase()
      .replace(whitespace, "");
    modified[key] = value;
  });
  return modified;
});

console.log(lo);
about 4 years ago · Juan Pablo Isaza Relatório

0

Here is my solution, use map function.

var arr = [
    {
      "Outdated Label (Key1)": "Outdated Label1",
      "Outdated Value": "Outdated Value"
    },
    {
      "Outdated Label (Key1)": "Outdated Label2",
      "Outdated Value": "Outdated Value2"
    },
    {
      "Outdated Label (Key1)": "Outdated Label3",
      "Outdated Value": "Outdated Value3"
    },
    {
      "Outdated Label (Key1)": "Outdated Label4",
      "Outdated Value": "Outdated Value4"
    }
  ];

const expectedJson = arr.map(function(item) {
  for(const key of Object.keys(item)) {
    const lowerCaseKey = key.toLowerCase().replace(/\s/g, '');
    if (lowerCaseKey !== key) {
      item[lowerCaseKey] = item[key];
      delete item[key];
    }
  }
  return item;
})

console.log(expectedJson)
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