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

103
Views
destructuring object into multiple object assignment

I have an object like so:

const obj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4,
  e: 5
}

I want to create multiple object literals out of it. So:

const obj1 = {
 a: 1,
 b: 2
}

const obj2 = {
  c: 3,
  d: 4,
  e: 5
}

Is it possible to achieve this using an object destructing assignment?

Something like:

const { {a, b}: obj1, {c, d, e}: obj2} = obj;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Is it possible to achieve this using object destructuring assignment?

No, it is not possible. However, you can write a function to split an object by its property names into multiple objects like this:

function splitObject (obj, ...propertyGroups) {
  return propertyGroups.map(group => {
    const o = {};
    for (const key of group) o[key] = obj[key];
    return o;
  });
}

const obj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4,
  e: 5,
};

const [obj1, obj2] = splitObject(obj, ['a', 'b'], ['c', 'd', 'e']);

console.log({obj, obj1, obj2});

By returning the sub-objects in a tuple, you have the freedom to destructure it, naming each one according to your preference.

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!