-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
24 lines (19 loc) · 819 Bytes
/
Copy pathscript.js
File metadata and controls
24 lines (19 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const products = [
{ id: 1, name: "Product 1", price: 100 },
{ id: 2, name: "Product 2", price: 200 },
{ id: 3, name: "Product 3", price: 300 },
{ id: 4, name: "Product 4", price: 400 },
{ id: 5, name: "Product 5", price: 500 },
{ id: 6, name: "Product 6", price: 600 },
];
// Remove product from array by ID without mutating the original array
function removeProduct(id) {
const clonedProducts = [...products];
const index = clonedProducts.findIndex((product) => product.id === id);
if (index === -1) return clonedProducts; // If not found, return original clone
clonedProducts.splice(index, 1);
return clonedProducts;
}
// Example usage
console.log(removeProduct(3)); // Product 3 removed
console.log(products); // Original array intact