1

Here I have products which is a list of objects. After filtered, selectedProduct is still a list of objects and thus I have to use [0]. In C#, they have something called SingleOrDefault which return only a single object after run a LINQ query. In Javascript, do they have something similar?

const products = [{
    id: 5,
    productName: "Logitech Mouse",
    unitprice: 35
  },
  {
    id: 6,
    productName: "Logitech Keyboard",
    unitprice: 40
  }
];

selectedProduct = products.filter(
  prod => prod.id == 6
)

console.log(selectedProduct[0].productName);
Steve
  • 2,963
  • 15
  • 61
  • 133

0 Answers0