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

92
Visualizações
.map usage in an object

just wondering if someone could point me in the right direction of .map functionality. This is unfortunately something I'm struggling to get my head around.

If I had an object, lets say the following:

const myPetsAndFood = {
    pets:[
    { 
        species: "dog",
        breed: "Labrador",
        age: 12
    },
    {
        species: "cat",
        breed: "unknown",
        age: 7,
    },
    {
        species: "fish",
        breed: "goldfish",
        age: 1,
    }
],
    food: [
        {
            dogfood: 15.00,
        },
        {
            catfood: 11.00,
        },
        {
            fishfood: 4.00,
        }
    ],
}; 

Could anyone explain how I'd utilise .map to obtain the data values of age and price if possible please? A brief explanation or example is more than suffice, I'd appreciate any time/input possible. In all probability, I'll be sat here reading and trying to figure it out in the mean time.

If you got this far - Thank you for your time.

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

0

So the .map can only be used with arrays. This way you can not do something similar to:

myPetsAndFood.map()

Let's say you want do console.log the age. You would have to get the array first. So:

myPetsAndFood.pets.map((pet) => {
   console.log(pet.age)
})

And it would print 12, followed by 7 followed by 1. If you want to store it inside an array you can create an array and use .push("//infos wanted to be pushed//")

about 4 years ago · Juan Pablo Isaza Relatório

0

So you have imbricated arrays here. This makes it so you have to go into your wanted array first before being able to execute your map.

For example: myPetsAndFood.pets.map(function)

The way that .map works is it executes your function on every element in your array and returns an array with the equivalency(source).

Therefore, in order to get the age of every pet, you have to tell your function to get your age property of your objects.

For example: myPetsAndFood.pets.map((pet) => pet.age)

This will return an array containing only the age of every one of your pets.

Now the problem with this is your second array. We cannot call the .map function on that array because your different properties don't have the same name. Therefore, your .map won't have any common ground to return a sensible array.

We can fix this issue by splitting your one variable into two: name and price for example. After this change, we can now call the .map on your array properly by telling it which property you need.

For example: myPetsAndFood.foods.map((food) => food.price)

Below is a full code snippet which should show off the above description.

const myPetsAndFood = {
  pets:[
    { 
      species: "dog",
      breed: "Labrador",
      age: 12
    },
    {
      species: "cat",
      breed: "unknown",
      age: 7,
    },
    {
      species: "fish",
      breed: "goldfish",
      age: 1,
    }
  ],
  foods: [
    {
      name: "dog",
      price: 15.00,
    },
    {
      name: "cat",
      price: 11.00,
    },
    {
      name: "fish",
      price: 4.00,
    }
  ],
};

const catAge = myPetsAndFood.pets.map((pet) => pet.age)
const foodPrice = myPetsAndFood.foods.map((food) => food.price)


console.log(catAge)
console.log(foodPrice)

about 4 years ago · Juan Pablo Isaza Relatório

0

map is a method of arrays, it doesn't exist on objects. You could use it on the arrays within the object ( myPetsAndFood.pets.map( /* ... */ ) ) but you'd have to use a for loop or some other technique to parse each item in the object.

An example of how to use the map function for one of your arrays:

const agesArray = myPetsAndFood.pets.map((item) => {
    return item.age;
});
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