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

190
Visualizações
Getting Own object keys with no built-in methods

There is a task/challenge to get all object keys as an array. The issue is all build-in arrays and object methods are restricted ( no Object.keys() ). I can use for in loop

function getKeys(obj) {
  const keysArr = [];

  for (const key in obj) {
    if(obj.hasOwnProperty(key)){ //restricted native object method
      keysArr.push(key);
    }
  }

  return keysArr;
}

But there is an active linter rule https://eslint.org/docs/latest/rules/guard-for-in that requires me to put a guard on a key to check if it's not coming from the object's prototype. And I can not find any way to do it, because .hasOwnProperty is a method and as far as I know there is no alternative to it because it is browser's native code. Does anyone have an idea of how to get only the own object properties in this case?


Update: as mentioned in an answer below what they probably wanted me to do is to loop through a shallow spread copy of an object const key in {...obj}. Does the job of removing values from the prototype, but does not fix linter error. Error the task itself. Thank you all for your answers

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can spread the object and loop over the result:

let obj = {foo: true}
let prototype = {bar: true}
Object.setPrototypeOf(obj, prototype)

const keysArr = [];
for (let key in {...obj}) {
  keysArr.push(key)
}

console.log(keysArr)

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