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

Handpicked posts just for you — based on your current read.

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!