Selected item from array with objects


#1

Hello

I’m very new in ionic 2

**I’m tryin’ to console.log the selected pizza ** i have tried like this
itemSelected(pizzas) {
console.log(“Selected Item”, this.pizzas);
}

but it returns two objects

<ion-list *ngFor=“let pizza of pizzas” (click)=“itemSelected(pizzas)”>


{{pizza.name}}


        <img [src]="pizza.image" class="pizza-image" />
        
        <div class="pizza_info">
            <p> {{pizza.ingredients}} </p>
            <p> {{ pizza.prize + pizza.currency }}</p>
        </div>
    </ion-card>
pizzas = [

{
  image: "https://austincoding.s3.amazonaws.com/uploads/photo/picture/9/avers.jpg",
  name: 'Pepperoni',
  ingredients: ["Parmesan cheese", "Extra sos", "Pepperoni"],
  prize: '5',
  currency: '€',
},
{
  image: "https://austincoding.s3.amazonaws.com/uploads/photo/picture/9/avers.jpg",
  name: 'Pershute',
  ingredients: ["Extra sos", "Pershute"],
  prize: '3',
  currency: '€',
}

#2

If you only want to log 1 pizza, do this:

itemSelected(pizza) {
console.log("Selected Item", pizza);
}

this.pizzas refers to the local pizza’s array you’ve created in your Class (pizzas = [] etc…). So if you’re doing

 itemSelected(pizza) {
    console.log("Selected item", this.pizzas);
}

it will only log the value of this.pizzas and not the pizza you’re trying to catch in your click handler.


#3

Thank you a billion times, i solved my problem.