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

148
Visualizações
Best functional-style syntax to build this object?

The below code achieves desired output. Is there a more elegant way to do this?

For example, is there some native javascript function like flatMap etc that would help?

(I know I could get rid of the intermediate variable pieces).

const config = {
    fieldName1: {
        validation: "schema1",
        value: "abcvalue here"
    },
    fieldName2: {
        validation: "schema2",
        value: "abcvalue here"
    },
}

// Desired output: 
// {
//     fieldName1: "schema1",
//     fieldName2: "schema2",
//     ...
// }
const extractValidation = (config) => {
    const pieces = Object.entries(config).map(
        ([key, val]) => ({
            [key]: val.validation
        })
    )
    return Object.assign({}, ...pieces)
}


extractValidation(config)
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

It's more concise, and I think prettier, to pair fromEntries with a map over .entries.

const config = {
    fieldName1: {
        validation: "schema1",
        value: "abcvalue here"
    },
    fieldName2: {
        validation: "schema2",
        value: "abcvalue here"
    },
}

const extractValidation = (config) => Object.fromEntries(
  Object.entries(config).map(([k,v]) => [k, v.validation])
);

console.log(extractValidation(config))

over 4 years ago · Santiago Trujillo Relatório

0

An alternative is to use reduce so you can skip the Object.assign:

Object.entries(config).reduce((o, [k, v]) => (o[k] = v.validation, o), {})
//=> {fieldName1: 'schema1', fieldName2: 'schema2'}
over 4 years ago · Santiago Trujillo Relatório

0

this is how I would do it. By leveraging Ramda, you can go point-free and use map to any Functor

const fn = R.map(R.prop('validation'));

const data = {
  fieldName1: {
    validation: "schema1",
    value: "abcvalue here"
  },
  fieldName2: {
    validation: "schema2",
    value: "abcvalue here"
  },
};

console.log(
  fn(data),
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.js" integrity="sha512-ZZcBsXW4OcbCTfDlXbzGCamH1cANkg6EfZAN2ukOl7s5q8skbB+WndmAqFT8fuMzeuHkceqd5UbIDn7fcqJFgg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

over 4 years ago · Santiago Trujillo 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