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

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

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

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