• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

130
Views
¿La mejor sintaxis de estilo funcional para construir este objeto?

El siguiente código logra el resultado deseado. ¿Hay una manera más elegante de hacer esto?

Por ejemplo, ¿hay alguna función javascript nativa como flatMap etc. que pueda ayudar?

(Sé que podría deshacerme de las pieces variables intermedias).

 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 3 years ago · Santiago Trujillo
3 answers
Answer question

0

Es más conciso, y creo que más bonito, emparejar fromEntries con un mapa sobre .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 3 years ago · Santiago Trujillo Report

0

Una alternativa es usar reduce para que pueda omitir Object.assign :

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

0

así es como lo haría. Al aprovechar Ramda, puede ir sin puntos y usar el map para cualquier 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 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error