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

118
Vistas
Making javascript iteration similar to python

I would like to be able to call keys(), values(), and items() directly on the object, as a sort of shortform of Object.keys|values|entries(...). Here is what I have thus far:

let o = {
    a: 1,
    b: 2,
    keys()   {return Object.keys(this)},
    values() {return Object.values(this)},
    items()  {return Object.entries(this)}
};
for (let k of o.keys()) console.log(k);
for (let v of o.values()) console.log(v);
for (let [k,v] of o.items()) console.log(k,v);

It seems to get what what I want with the exception of I don't want that property to be enumerable. Two things related to this:

  1. What would be the proper way to make the item non-enumerable (so only a and b show up as the keys)? Would the following be good enough?

    for (let prop of ['keys', 'values', 'items'])
        Object.defineProperty(o, prop, {enumerable: false})
    // or
    ['keys','values','items'].forEach((prop,idx) => Object.defineProperty(o, prop, {enumerable: false}));
    
  2. Would the above (what I think is a convenience method) be considered a bad idea, and if so why?

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

0

You could add the helper methods to the Object.prototype object (basically the parent of all javascript objects) so that all objects have .keys(), .values(), and .entries() methods.

const obj = {
  a: 1,
  b: 2
};

for (let prop of ['keys', 'values', 'entries']) {
  Object.defineProperty(Object.prototype, prop, {
    enumerable: false,
    value: function() {
      return Object[prop](this);
    }
  });
}

console.log(obj);
console.log(obj.keys());
console.log(obj.values());
console.log(obj.entries());

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can edit the prototype methods, so that the change will be global.

Object.prototype.keys = function(){ 
  return Object.keys(this);
}

Object.prototype.entries = function(){ 
  return Object.entries(this);
}

Object.prototype.values = function(){ 
  return Object.values(this);
}

const test = {
  name: "bob"
};

console.log({
  keys: test.keys(), 
  values: test.values(), 
  entries: test.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