Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!