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

188
Views
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 answers
Answer question

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