Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda