Skip to content

Instantly share code, notes, and snippets.

@Sagor0078
Last active January 6, 2025 14:38
Show Gist options
  • Save Sagor0078/db7e215cea1690fc9864ba6e0f908336 to your computer and use it in GitHub Desktop.
Save Sagor0078/db7e215cea1690fc9864ba6e0f908336 to your computer and use it in GitHub Desktop.
// Array of products with their prices
const products = [
{ name: "Laptop", price: 999.99 },
{ name: "Mouse", price: 25.50 },
{ name: "Keyboard", price: 15.99 },
{ name: "USB Cable", price: 5.00 },
{ name: "Monitor", price: 199.99 },
{ name: "Headphones", price: 19.99 }
];
// Using filter and map to get names of products on sale, in uppercase
const onSaleProducts = products
.filter(product => product.price < 20) // Filter products that are on sale
.map(product => product.name.toUpperCase()); // Convert names to uppercase
console.log(onSaleProducts);
// output should be : ['USB CABLE', 'HEADPHONES']
# List of products with their prices
products = [
("Laptop", 999.99),
("Mouse", 25.50),
("Keyboard", 15.99),
("USB Cable", 5.00),
("Monitor", 199.99),
("Headphones", 19.99)
]
# Using list comprehension to get names of products on sale, in uppercase
on_sale_products = [name.upper() for name, price in products if price < 20]
print(on_sale_products)
# output should be : ['USB CABLE', 'HEADPHONES']
# Python and JavaScript can efficiently filter and transform data using list comprehensions or equivalent methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment