Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Utmaning: Sök Efter Objekt Med find() | Avancerade Arraymetoder och Transformationer
Javascript Datastrukturer

bookUtmaning: Sök Efter Objekt Med find()

Uppgift

  1. Den ursprungliga arrayen ges som products, innehållande objekt som representerar produkter med egenskaperna name, price och featured.
  2. Använd metoden find() för att hitta den första produkten i arrayen där egenskapen featured är satt till true.
  3. Om en utvald produkt hittas, logga dess namn och pris; annars, logga ett meddelande som indikerar att ingen utvald produkt hittades.
12345678910111213141516
const products = [ { name: "Laptop", price: 1200, featured: false }, { name: "Headphones", price: 150, featured: true }, { name: "Smartphone", price: 800, featured: false }, { name: "Camera", price: 1000, featured: true }, ]; const featuredProduct = ___.___((product) => ___ === true); if (featuredProduct) { console.log( `Featured product: ${___}, Price: $${featuredProduct.___}` ); } else { console.log(___); }
copy

Förväntad utdata:

Featured product: Headphones, Price: $150
  1. Använd metoden find() på arrayen products och ange en callback-funktion som kontrollerar om egenskapen featured för produktobjektet är true.
  2. Om en utvald produkt hittas, logga dess namn och pris; annars logga ett meddelande som indikerar att ingen utvald produkt hittades.
12345678910111213141516
const products = [ { name: "Laptop", price: 1200, featured: false }, { name: "Headphones", price: 150, featured: true }, { name: "Smartphone", price: 800, featured: false }, { name: "Camera", price: 1000, featured: true }, ]; const featuredProduct = products.find((product) => product.featured === true); if (featuredProduct) { console.log( `Featured product: ${featuredProduct.name}, Price: $${featuredProduct.price}` ); } else { console.log("No featured product found."); }
copy

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 6

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain how the find() method works in this context?

What happens if there are no featured products in the array?

Can you show how to modify the code to find all featured products instead of just the first one?

Awesome!

Completion rate improved to 2.27

bookUtmaning: Sök Efter Objekt Med find()

Svep för att visa menyn

Uppgift

  1. Den ursprungliga arrayen ges som products, innehållande objekt som representerar produkter med egenskaperna name, price och featured.
  2. Använd metoden find() för att hitta den första produkten i arrayen där egenskapen featured är satt till true.
  3. Om en utvald produkt hittas, logga dess namn och pris; annars, logga ett meddelande som indikerar att ingen utvald produkt hittades.
12345678910111213141516
const products = [ { name: "Laptop", price: 1200, featured: false }, { name: "Headphones", price: 150, featured: true }, { name: "Smartphone", price: 800, featured: false }, { name: "Camera", price: 1000, featured: true }, ]; const featuredProduct = ___.___((product) => ___ === true); if (featuredProduct) { console.log( `Featured product: ${___}, Price: $${featuredProduct.___}` ); } else { console.log(___); }
copy

Förväntad utdata:

Featured product: Headphones, Price: $150
  1. Använd metoden find() på arrayen products och ange en callback-funktion som kontrollerar om egenskapen featured för produktobjektet är true.
  2. Om en utvald produkt hittas, logga dess namn och pris; annars logga ett meddelande som indikerar att ingen utvald produkt hittades.
12345678910111213141516
const products = [ { name: "Laptop", price: 1200, featured: false }, { name: "Headphones", price: 150, featured: true }, { name: "Smartphone", price: 800, featured: false }, { name: "Camera", price: 1000, featured: true }, ]; const featuredProduct = products.find((product) => product.featured === true); if (featuredProduct) { console.log( `Featured product: ${featuredProduct.name}, Price: $${featuredProduct.price}` ); } else { console.log("No featured product found."); }
copy

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 6
some-alt