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

358
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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