DeveloperBreeze

JavaScript Sort Array of Objects by Property

// Array of products with id, name, and price
const products = [
    { id: 3, name: 'Laptop', price: 1200 },
    { id: 1, name: 'Smartphone', price: 500 },
    { id: 2, name: 'Tablet', price: 300 }
];

// Sort the products array by price in ascending order
const sortedProducts = products.sort((a, b) => a.price - b.price);

// Display the sorted products
console.log('Sorted Products:', sortedProducts);

Continue Reading

Discover more amazing content handpicked just for you

Tutorial
javascript

Non-Primitive Data Types (Objects, Arrays, and Functions)

  • Accessing Elements:
  console.log(fruits[0]); // "apple"

Dec 11, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!