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

103
Visualizações
Best way to merge two objects on a matching key-value pair

I'm having trouble making two objects into one on a matching key.

I have two objects coming from 2 apis and there is one matching key in the objects.

I want to iterate over them and if the storeId matches in both objects, I want to merge the two together as seen in the perfectObject.

I have tried the spread operator, Object.assign, for...in loop instead of the for loop seen here, but found close to none success.

Thanks for the help!

const logistics = [{
  logisticsId: "L5E69E26D8FCAE",
  storeId: 409388,
  logisticsDate: "2020-03-12T07:19:09.000Z",
}, ];

const stores = [{
  storeId: 409388,
  ka: 0,
  country: "ru",
  name: "test",
  city: "Moscow",
  cxw: 1,
  cx: 1,
  plz: 22448,
}, ];

const perfetObject = {
  storeId: 409388,
  ka: 0,
  country: "ru",
  name: "test",
  city: "Moscow",
  cxw: 1,
  cx: 1,
  plz: 22448,
  "storeId": 409388,
  logisticsId: "L5E69E26D8FCAE",
  storeId: 409388,
  logisticsDate: "2020-03-12T07:19:09.000Z",
};
"logisticsId": "L5E69E26D8FCAE",
let d = {};
}
for (let i = 0; i < logistics.length; ++i) {
  for (let k = 0; k < stores.length; ++k) {
    if (logistics.storeId === stores.storeId) {
      d = {
        ...stores.name,
        ...stores.city,
        ...logistics.logisticsId,
      };
    }
  }
  let d = {}

  console.log(d);

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

0

Leaving aside the nested for loop (there are better data structures for you to take advantage of that make this unnecessary), the easiest method would be to use Object.assign() or the spread operator, especially if the key names are guaranteed to never conflict.

const logistics = [{
  logisticsId: "L5E69E26D8FCAE",
  storeId: 409388,
  logisticsDate: "2020-03-12T07:19:09.000Z",
}, ];

const stores = [{
  storeId: 409388,
  ka: 0,
  country: "ru",
  name: "test",
  city: "Moscow",
  cxw: 1,
  cx: 1,
  plz: 22448,
}, ];

for (let i = 0; i < logistics.length; ++i) {
  for (let k = 0; k < stores.length; ++k) {
    if (logistics[i].storeId === stores[k].storeId) {
      console.log(Object.assign({}, logistics[i], stores[k]));
    }
  }
}

This assumes all store IDs are valid (e.g. have the expected store data) and all logistics elements have a valid store defined.

about 4 years ago · Juan Pablo Isaza Relatório

0

This is a working example answer from a reddit user u/albedoa:

const storesByStoreId = stores.reduce(
  (obj, store) => {
    if (!obj.hasOwnProperty(store.storeId)) {
      obj[store.storeId] = store;
    }

    return obj;
  },
  {}
);

const perfectArray = logistics.map(logistic => ({
  ...storesByStoreId[logistic.storeId],
  ...logistic
}));

This way, it combines the two as supposed to, or returns the object itself if there is no match.

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