Angular 2 TypeScript how to find element in Array

前端 未结 7 780
抹茶落季
抹茶落季 2021-01-29 21:35

I have a Component and a Service:

Component:

7条回答
  •  清酒与你
    2021-01-29 22:00

    You could combine .find with arrow functions and destructuring. Take this example from MDN.

    const inventory = [
      {name: 'apples', quantity: 2},
      {name: 'bananas', quantity: 0},
      {name: 'cherries', quantity: 5}
    ];
    
    const result = inventory.find( ({ name }) => name === 'cherries' );
    
    console.log(result) // { name: 'cherries', quantity: 5 }
    

提交回复
热议问题