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

97
Vistas
Conditionally assign from part of an object

I think there's an elegant way to do this (maybe using destructuring), but I'm stumped so far. I'd like to assign some of the keys and values from one object to another, conditionally based on a boolean.

function bigObject() {
  return { /* an object with lots of keys and values, lots */ }
} 

function myFunction(aBool) {
  // I want smallObject to be either empty or contain a subset of bigObject, based on aBool
  // here's where I am stuck
  let smallObject = aBool ? { keyA, keyB } = bigObject() : {};
}

JS complains that keyA is a reference error, and I can see how, even if I had this right, I'd just end up creating keyA and keyB variables, not smallObject as I want.

I know I can do this:

let smallObject = {};
if (aBool) {
  let bigObject = bigObject();
  smallObject.keyA = bigObject.keyA;
  smallObject.keyB = bigObject.keyB;
  smallObject.keyC = bigObject.keyC;
  // 5 of these
}

But that sure is a lot of stuff to write when I think there's a way I can write just the props names.

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

0

It's not a problem to use temporary destructuring variables. Just use them again to build the target object. If you want it in one expression, you can use an inline function that performs the destructuring in its parameter variables, and then returns the new object:

let smallObject = aBool ? (
    ({ keyA, keyB }) => ({ keyA, keyB })
) (bigObject()) : {};

const bigObject = () => ({ keyA: 1, keyB: 2, keyC: 3, keyD: 4});

let aBool = true;

let smallObject = aBool ? (
    ({ keyA, keyB }) => ({ keyA, keyB })
) (bigObject()) : {};

console.log(smallObject);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use an IIFE that returns a destructured object.

const bigObject = {
  keyA: 'a',
  keyB: 'b',
  keyC: 'c',
};

const getObj = (aBool) =>
  aBool
    ? (({ keyA, keyB }) => ({ keyA, keyB }))(bigObject)
    : {};

console.log(getObj(true));

Or you could write your own "plucking" function to return a new object based on the original:

const bigObject = {
  keyA: 'a',
  keyB: 'b',
  keyC: 'c',
};

const pluck = (obj, callback) => callback(obj);

const getObj = (aBool) =>
  aBool
    ? pluck(bigObject, ({ keyA, keyB }) => ({ keyA, keyB }))
    : {};

console.log(getObj(true));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Option 1

if (aBool) smallObject = { ...smallObject, ...bigObject}; 

Option 2

if (aBool) {
   Object.entries(bigObject).forEach(([key, value]) => smallObject[key] = value);
}
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