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

74
Visualizações
Concatinating two objects deeply

I have these two objects:

const tmp = {
  pl: {
    translation: {
      states: {
        foo: { name: 'bar' },
      },
    },
  },

  en: {
    translation: {
      states: {
        foo: { name: 'bar' },
      },
    },
  },
};
const tmp2 = {
  pl: {
    translation: {
      states: {
        foz: { name: 'baz' },
      },
    },
  },

  de: {
    translation: {
      states: {
        foo: { name: 'bar' },
      },
    },
  },
};

How can I concatenate them? the pl part is fluent, it can change so it has to be dynamic.

I was thinking about doing it recursively with a mix of Object.keys, but it seems like an overkill.

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

0

lodash merge will do the trick here:

const tmp = {
  pl: { translation: { states: { foo: { name: 'bar' } } } },
  en: { translation: { states: { foo: { name: 'bar' } } } },
};
const tmp2 = {
  pl: { translation: { states: { foz: { name: 'baz' } } } },
  de: { translation: { states: { foo: { name: 'bar' } } } },
};

console.log(_.merge(tmp, tmp2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório

0

There you go:

function merge(o1,o2){
    const result = {}
    for(const key of Object.keys(o1)) result[key] = key in o2 ? merge(o1[key],o2[key]) : o1[key];
    for(const key of Object.keys(o2)) if(!(key in o1)) result[key] = o2[key];
    return result; 
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I would suggest a more standard approach, generic and especially without libraries:

const extend = (isDeep, objects) => {

// Variables
let extended = {};
let deep = isDeep;

// Merge the object into the extended object
const merge = function (obj) {
    for (let prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            if (deep && Object.prototype.toString.call(obj[prop]) === '[object Object]') {
                // If we're doing a deep merge and the property is an object
                extended[prop] = extend(deep, [extended[prop], obj[prop]]);
            } else {
                // Otherwise, do a regular merge
                extended[prop] = obj[prop];
            }
        }
    }
};

// Loop through each object and conduct a merge
for (let argument of objects) {
    merge(argument)
}
return extended;
};

And you can use it simply calling:

extend(true, [tmp, tmp2])

The first boolean parameter is used to perform a deep merge or a regular merge.

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