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

168
Visualizações
Merge two object arrays by matching partial value

I have two objects of arrays like the below:

person: {
    0: {
        firstName: John
        lastName: Doe
        avatar: https://sitename.com/filename.jpg
    },
    etc..
}
image: {
    0: {
        Key: filename.jpg
        files: {
            srcSet: 'filename.jpg 256w'
        }
    }
}

My goal is to match the objects up via the value found in avatar with the value in Key, and after matching merge the objects of arrays into a new array structure like the below:

person: {
    0: {
        firstName: John
        lastName: Doe
        avatar: https://sitename.com/filename.jpg
        Key: filename.jpg
        files: {
            srcSet: 'filename.jpg 256w'
        }
    },
    etc..
}

My gut tells me some sort of .filter and .map chained to the filter could help create the new array of objects, however, am unsure how to match a partial value.

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

0

You can do something like this by calling Array.prototype.map() on Object.values(yourobject)

After calling map on values of the original object, you can check if an element with the same key exists in your second object, if it does use the spread operator to create a new resultant object with the combined properties and then return it

const person = {
    0: {
        firstName: "John",
        lastName: "Doe",
        avatar: "https://sitename.com/filename.jpg"
    },
}
const image = {
    0: {
        Key: "filename.jpg",
        files: {
            srcSet: 'filename.jpg 256w'
        }
    }
}

const imageKey = (avatar) => {
    const arr = avatar.split('/');
    return arr[arr.length - 1];
}

const val = Object.values(person).map(v => {
    const found = Object.values(image).find(x => x.Key == imageKey(v.avatar));
    if (found) 
        return {...v, ...found };
    else 
        return {...v}
})

console.log(val);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can't use map over objects, but you can use Object.values(object) to get its values

const person = {
    0: {
        firstName: "John",
        lastName: "Doe",
        avatar: "https://sitename.com/filename.jpg"
    },
}
const image = {
    0: {
        Key: "filename.jpg",
        files: {
            srcSet: 'filename.jpg 256w'
        }
    }
}

const getPersonImageKey = (person) => {
  return person.avatar.split("/").at(-1);
}

const mergedObjs = Object.values(person).map(person => {
  const relatedImage = Object.values(image).find(image => image.Key === getPersonImageKey(person));
  return {...person, ...relatedImage};
})

const finalObj = {};
mergedObjs.forEach((obj, idx) => finalObj[idx] = obj); 

console.log(finalObj);

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