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

198
Views
Flatten Object in-place in JavaScript

In a recent interview, I was asked to create a flat method which flattens an object. I tried to implement it like following, but it doesn't work:

Object.defineProperty(Object.prototype, 'flat', {
  enumerable: false,
  writable: true,
  configurable: false,
  value: function() {
      function flatten(obj) {
      for(let key in obj) {
        if(obj.hasOwnProperty(key)) {
          if(typeof obj[key] === 'object') {
            obj[key] = flatten(obj[key])
          }
          else {
           obj[key] = obj[key];
          }
        }
      }
      return obj;
    }
  flatten(this);
  }
});

var obj = { a: 1, b: 2, c: { d: { e: 5 }, f: 6 } }
obj.flat();
console.log(obj); // should return { a: 1, b: 2, e: 5, f: 6 }

What's wrong in my code?

PS: Other post on SO for flattening Object don't do it in-place, which is the requirement in this case.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You're creating the same recursive object structure here:

obj[key] = flatten(obj[key])

You need to put the nested properties onto the original object instead. You also need to remove the intermediate properties from the original object, so that you're left with only the properties at the end.

I'd also highly, highly recommend against mutating built-in prototypes, especially Object.prototype - it can lead to compatibility issues and confusing behavior, especially when libraries are involved. Things will break eventually as a result. Use a plain function instead.

const flatten = (base, obj = base) => {
  for (const [key, value] of Object.entries(obj)) {
    if (typeof value === 'object' && value !== null) {
      delete obj[key];
      flatten(base, value);
    } else {
      base[key] = value;
    }
  }
};
var obj = {
  a: 1,
  b: 2,
  c: {
    d: {
      e: 5
    },
    f: 6
  }
}
flatten(obj);
console.log(obj);

about 4 years ago · Juan Pablo Isaza Report

0

With help from the other answer, I got the exact code I wanted:

Object.defineProperty(Object.prototype, 'flat', {
  enumerable: false,
  writable: true,
  configurable: false,
  value: function() {
      function flatten(base, obj) {
        for(let key in obj) {
          if(obj.hasOwnProperty(key)) {
            if(typeof obj[key] === 'object' && obj[key] !== null) {
              flatten(base, obj[key]);
              delete obj[key];
            }
            else {
             base[key] = obj[key];
            }
          }
        }
      }
    flatten(this, this);
  }
});

var obj = { a: 1, b: 2, c: { d: { e: 5 }, f: 6 } }
obj.flat();
console.log(obj); // returns { a: 1, b: 2, e: 5, f: 6 }

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!