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

152
Visualizações
How to assign values from an object to properties in an array, conditionally

I need to assign values from an array of objects, to an object, with different keys.

In the JavaScript app, I'm working with, I get an array of objects returned similar to below:

urlArrayOfObjects = [ 

    {url: 'https://amazon.co.uk', country: 'uk'},
    {url: 'https://amazon.com', country: 'usa'},
    {url: 'https://amazon.ca', country: 'canada'},
    {url: 'https://amazon.es', country: 'spain'}
]

The object is the something like the following:

urlTables = {
    short_url_id: "",
    uk_url: "",
    usa_url: "",
    canada_url: "",
    irland_url: "",
    spain_url: "",
    germany_url: "",
    default_country: "",
    default_url: "",
    customer_id: ""
}

I need to extract values from urlArrayOfObjects and insert them into the appropriate properties in the urlTables (And if there are no values, if should default to null

For e.g. in the above case, urlTables should look like the following after I'm done with it:

urlTables = {
    short_url_id: null,
    uk_url: "https://amazon.co.uk",
    usa_url: "https://amazon.com",
    canada_url: "https://amazon.ca",
    irland_url: null,
    spain_url: "https://amazon.es",
    germany_url: null,
    default_country: null,
    default_url: null,
    customer_id: null}

What is the best way of going about achieving this? Thank you so much for your help!

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

0

This is an area where JavaScript, as compared to a strongly typed language really shines. You can simply fetch the values and use them as field names.

For example:

urlTables[urlArrayOfObjects[0].country + "_url"] = urlArrayOfObjects[0].url;

The idea is the same for everything else so you just need to loop.

Here's an example loop:

for(var item of urlArrayOfObjects){
    urlTables[item.country + "_url"] = item.url;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I would do it like this:

let urlArrayOfObjects = [ 

    {url: 'https://amazon.co.uk', country: 'uk'},
    {url: 'https://amazon.com', country: 'usa'},
    {url: 'https://amazon.ca', country: 'canada'},
    {url: 'https://amazon.es', country: 'spain'}
]

let urlTables = {
    short_url_id: null,
    uk_url: null,
    usa_url: null,
    canada_url: null,
    irland_url: null,
    spain_url: null,
    germany_url: null,
    default_country: null,
    default_url: null,
    customer_id: null
}


console.log(
    urlArrayOfObjects.reduce((carry, current) => {
        return {
            ...carry,
            [current.country + '_url']: current.url
        }
    }, urlTables)
);

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