Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda