用JS实现商品数据库增删改查的代码
// 创建一个商品数据库
let products = [];
// 添加商品
function addProduct(name, price) let product = {
id: products.length 1,
name: name,
price: price
};
products.push(product);
}
// 删除商品
function deleteProduct(id) {
for (let i = 0; i < products.length; i ) {
ifproducts[i].id === id) {
.splice(i, 1);
break;
}
}
}
// 修改商品
function updateProduct(id, name, price) {
for (let i = 0; i < products.length; i ) {
if (products[i].id === id) {
products[i].name = name;
products[i].price = price;
break;
}
}
}
// 查询商品
function findProduct(id) {
for (let i = 0; i < products.length; i ) {
if (products[i].id === id) {
return products[i];
}
}
return null;
}
// 测试代码
addProduct("Apple", 10);
addProduct("ana", 5);
console.log(products);
deleteProduct(1);
console.log(products);
updateProduct(2, "Orange", 8);
console.log(products);
product = findProduct(2);
console.log(product);
转载请注明出处或者链接地址:https://www.qianduange.cn//article/8592.html
相关文章
-
运行npm error code ENOENTnpm error syscall opennpm error path C:\Users\ultra\Desktop\Vue-Project\pac
-
解决npm install 报错 “npm err code 1“
-
【常见错误】npm ERR! code CERT_HAS_EXPIRED & errno CERT_HAS_EXPIRED
-
vue前端页面弹出红色报错遮罩层 Uncaught runtime errors:at handleError (webpack-internal:///./node_modules/webpack
-
基于CSS3媒体查询的响应式旅游网站设计与实现-计算机毕设 附源码 12755
-
Postman导出JSON文件轻松转换为HTML或Markdown
-
python requests编写 api接收json
-
npm 安装详细教程 最后附上 Node版本管理Nvm的安装教程
-
npm安装指定版本pnpm
-
ERROR:This version of pnpm requires at least Node.js vXXX 的解决方案
发布的文章
运行npm error code ENOENTnpm error syscall opennpm error path C:\Users\ultra\Desktop\Vue-Project\pac
2024-08-27 09:08:17
前端提高篇(102):jQuery高级方法callbacks、deferred
2024-05-09 11:05:34
解决npm install 报错 “npm err code 1“
2024-06-06 10:06:47
【常见错误】npm ERR! code CERT_HAS_EXPIRED & errno CERT_HAS_EXPIRED
2024-04-22 09:04:34
vue前端页面弹出红色报错遮罩层 Uncaught runtime errors:at handleError (webpack-internal:///./node_modules/webpack
2024-03-29 15:03:20
npm ERR! code CERT_HAS_EXPIRED npm ERR! errno CERT_HAS_EXPIRED npm ERR! request to https://registry.
2024-04-20 17:04:38
JQuery中的load()、$
2024-05-10 08:05:15
《WEB前端框架开发技术》HTML5响应式旅游景区网站——榆林子州HTML CSS JavaScript (1)
2024-10-30 21:10:12
大家推荐的文章