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

125
Visualizações
passing the property of an object to another object as a key

Lets say I have an object with image urls.

const imageURL = {
  brakePad: "some_url_string",
  tieRodEnd: "some_url_string",
  rackEnd: "some_url_string",
};

I also have an array of products.

const products = [
      {
        type : 'brakePad',
        brand : 'Akebono',
        img : // i would like to pass the type value to the 
              // imageURL object to get the url ; in this instance 
              // brakePad
              // i know i can pass it as img: imageURL['brakePad']
              // but that is not what i am trying to achieve.
              // is there any way i could extract the value of 
              // the type property and pass it to the imageURL 
              //object as the key 
      } ,
      {
        // similar object
      },
      
];

i would like to pass the type value to the imageURL object to get the url. i know i can pass it as img: imageURL['brakePad'] but that is not what i am trying to achieve. is there any way i could extract the value of the type property and pass it to the imageURL object as the key Thanks

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

0

You can use map to get an array of URL's from iterating over the products array.

const result = imageURL[products[0].type];

const products = [
  {
    type: "brakePad",
    brand: "Akebono",
    img: "url",
  },
  {
    type: "tieRodEnd",
    brand: "Akebono",
    img: "url",
  },
];

const imageURL = {
  brakePad: "some_url_string",
  tieRodEnd: "some_url_string",
  rackEnd: "some_url_string",
};

const result = products.map((o) => imageURL[o.type]);
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can iterate over the products array and then use the type value for each product,
e.g using for...of loop,

for (let product of products) {
  // here you have access to individual product
  imageURL[product.type] // brakepad, tierodend ...
}

or conventional for loop:

for (let i = 0; i < products.length; i++) {
  const type = products[i].type
  imageURL[type]
}

If you want to add the image URL to the product objects you can map over the products array e.g,

const imageURL = {
  brakePad: "some_url_string",
  tieRodEnd: "some_url_string",
  rackEnd: "some_url_string",
}

const products = [
  {
    type: 'brakePad',
    brand: 'Akebono',
  },
  {
  type: 'tieRodEnd',
  brand: 'MONSTER',
  }
]

const newProducts = products.map((product)=> {
  return {...product, img: imageURL[product.type]}
})

console.log(newProducts)

You can read more about map method here at mdn

Or if you would like to add to the existing objects instead of creating a new array using map, then:

for (let product of products) {
  product.img = imageURL[product.type]
}
console.log(products)
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