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

199
Vistas
How to create new function to change the value based on the previous one?

So, I have this:

const someFunction = () => {
  const facets = {
    names: {
      John: true,
      Mary: true
    },
    nationalities: {
      US: true,
      CA: true
    }
  };
  let final = {
    names: Object.keys(facets.names).join(" | "),
    nat: Object.keys(facets.nationalities).join(" | ")
  };
  return final;
};
let whatever = someFunction();
console.log(whatever);

How can I create another function to return this path and show the original value?

The rookie question, I know.

Thank you

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

0

I'd suggest passing the facets object to the function as a parameter, you can then easily write the input and output of the function:

        
const someFunction = (input) => {
    let final = {
      names: Object.keys(input.names).join(' | '),
      nat: Object.keys(input.nationalities).join(' | '),
    }
  return final;
}

const facets = { names: { John: true, Mary: true, }, nationalities: { US: true, CA: true, }, }
const output = someFunction(facets)
console.log('Input:', facets);
console.log('Output:', output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

Solution #1: Declare facets on module level, for accessing to it outside of someFunction:

const facets = {
  names: {
    John: true,
    Mary: true
  },
  nationalities: {
    US: true,
    CA: true
  }
};
// Your method: do the same, but simplified
const someFunction = () => ({
  names: Object.keys(facets.names).join(" | "),
  nat: Object.keys(facets.nationalities).join(" | ")
});
// Reverted method: just return initial value
const revertFuntion = () => facets

Solution #2: if you need to revert any value returned by someFunction to it's inital state, you can do something like this:

const revertChanges = obj => {
  for (const key in obj) {
    const res = {}
    obj[key].split(' | ').forEach(value => {
      res[value] = true;
    })
    obj[key] = res;
  }
  return obj;
}

let whatever = someFunction();
console.log(whatever);
// {
//   "names": "John | Mary",
//   "nat": "US | CA"
// }
let reverted = revertChanges(whatever);
console.log(reverted);
// Initial facets
about 4 years ago · Juan Pablo Isaza Denunciar

0

Not sure what you mean, but if you need both facets and final you can return an Object.

const someFunction = (facets, unify) => {
  const result = {
    names: Object.keys(facets.names).join(" | "),
    nat: Object.keys(facets.nationalities).join(" | ")
  };
  return unify ? {...result, original: facets } : {original: facets, result};
};

const facets = {
  names: {
    John: true,
    Mary: true
  },
  nationalities: {
    US: true,
    CA: true
  }
};
const whatever = someFunction(facets);
const pre = document.querySelector(`pre`);

pre.textContent = `Object with original/result properties: ${
  JSON.stringify(whatever, null, 2)}`;

// or include facets as 'original' within the return value
pre.textContent += `\n\nSingle object: ${JSON.stringify(someFunction(facets, true), null, 2)}`;
<pre></pre>

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