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

346
Views
Is there a way of destructuring an object with the unused as a variable?

So basically, can the unused part of the destructured object be condensed into a variable?

This obviously won't work because "toss" is already declared,

THIS IS JUST TO ILLUSTRATE THE QUESTION

const obj = { a:1, b:2, c:3, d:4 }
const toss = {b,c};
const {...toss, ...keep} = obj;
console.log(keep);  // { a:1, d:4 }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If toss is meant to be a list of keys to ignore - then you can define it as an array of strings - rather than the invalid way you defined it that would throw an error

Then it's a simple one liner (there's probably other ways to do this, as there always is in javascript, this is the first that came to mind though)

const obj = { a: 1, b: 2, c: 3, d: 4 }
const toss = ['b', 'c'];
const keep = Object.fromEntries(Object.entries(obj).filter(([k]) => !toss.includes(k)))
console.log(keep);

however, even simpler, since your code is all hard coded anyway

const obj = { a: 1, b: 2, c: 3, d: 4 }
const {b, c, ...keep} = obj;
console.log(keep);

about 4 years ago · Juan Pablo Isaza Report

0

Try this.

const _0 = {'a': 1, 'b': 2, 'c': 3, 'd': 4};
const _1 = {'a': 1, 'b': 2, 'c': 4};
const _2 = function(_0, _1) {
    let _2 = Object.keys(_0);
    let _3 = _2.length;
    let _4 = '';
    let _5 = {};
    let _6 = 0;

    while (_6 !== _3) {
        _4 = _2[_6++];

        if (typeof _1[_4] === 'undefined' || _0[_4] !== _1[_4]) {
            _5[_4] = _0[_4];
        }
    }

    return _5;
};
const _3 = _2(_0, _1);
console.log(_3);

This allows destructuring with {'key': value} pair comparisons instead of an array of keys.

A while() loop with Object.keys() is also faster and more browser-compatible than Object.entries().

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!