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

197
Visualizações
How to modify & replace the key an object by comparing it with other objects?

I have an array of object which consists some id's as a key.

const sampleObj1 = {0011:[{},{}], 0022:[{}, {}], 0033:[{},{}]}
const sampleObj2 = [{id:0011, name:'test1'}, {id:0022, name:'test2'}, {id:0033, name:'test3'}]

I want to compare sampleObj1 key with the sampleObj2 id value and then need to create new obj some like below.

const newObjByComparing = {test1:[{},{}], test2:[{},{}], test3:[{},{}]} //desired result 

I have tried using for loop but not able to get the desired compared value.

const fromRdx = sampleObj2; 
const fromApi = sampleObj1; 
const keys = Object.keys(fromApi);
const newObjAfterComparision = {};

for (let i in fromRdx) {
    newObjAfterComparision[fromRdx[i]] = fromApi[keys[i]];
    fromApi[fromRdx[i]] = fromApi[keys[i]];
    delete fromApi[keys[i]];
}

console.log(newObjAfterComparision)
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The below may be one possible solution to achieve the desired objective:

Code Snippet

const transformObj = (obj, arr) => arr.reduce((f, i) => ({
  ...f,
  [i.name]: obj[i.id]
}), {});

const sampleObj = {
  "0011": [{}, {}],
  "0022": [{}, {}],
  "0033": [{}, {}]
};
const sampleArr = [{
  id: "0011",
  name: "test1"
}, {
  id: "0022",
  name: "test2"
}, {
  id: "0033",
  name: "test3"
}];

console.log(transformObj(sampleObj, sampleArr));

Expalanation

  • Use .reduce() to iterate over the sampleArr
  • For each element populate a result obj (named f)
  • The key of the object will be the element's name and value will be the value from the sampleObj (where key is element's id)
about 4 years ago · Juan Pablo Isaza Relatório

0

Another approach using map and Object.fromEntries

const sampleObj1 = {'0011':[{},{}], '0022':[{}, {}], '0033':[{},{}]}
const sampleObj2 = [{id:'0011', name:'test1'}, {id:'0022', name:'test2'}, {id:'0033', name:'test3'}]


let y = Object.fromEntries(sampleObj2.map(({id,name}) => [name,sampleObj1[id]]))

console.log(y)

about 4 years ago · Juan Pablo Isaza Relatório

0

I Have a simple solution for you

const sampleObj1 = {0011:[{},{}], 0022:[{}, {}], 0033:[{},{}]}
const sampleObj2 = [{id:0011, name:'test1'}, {id:0022, name:'test2'}, {id:0033, name:'test3'}]

const keys = Object.keys(sampleObj1);
const newObjByComparing = sampleObj2.reduce((acc, cur, index) => {
    acc[cur.name] = sampleObj1[keys[index]];
    return acc;
}, {});

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