当前位置:网站首页>js商品总价格、最高价格商品、排除重复商品[初版]
js商品总价格、最高价格商品、排除重复商品[初版]
2022-08-02 16:12:00 【编程小橙子】

javascript-illustration.png
实现商品总价格、最高价格商品、排除重复商品
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body></body>
</html>
<script>
let card = [
{ name: "huawei", price: 6000 },
{ name: "iphone", price: 8000 },
{ name: "nox", price: 2400 },
{ name: "ipad", price: 1650 },
{ name: "ipad", price: 1650 },
{ name: "xiaomi", price: 2300 },
{ name: "xiaomi", price: 2300 },
];
// 商品总价
function countPrice(count) {
return count.reduce((pre, cur) => {
return (pre += cur.price);
}, 0);
}
let count_price = countPrice(card);
console.log(count_price); //20350
// 最高价格商品
function maxPrice(max) {
return max.reduce((pre, cur) => {
return pre.price > cur.price ? pre : cur;
});
}
let max_price = maxPrice(card);
console.log(max_price); // {name: "iphone", price: 8000}
// 过滤重复商品
function filterCard(filt) {
return filt.reduce((pre, cur) => {
let find_card = pre.find((k) => k.name == cur.name);
if (!find_card) pre.push(cur);
return pre;
}, []);
}
let filter_card = filterCard(card);
console.log(
filter_card
); /*
[{name: "huawei", price: 6000},
{name: "iphone", price: 8000},
{name: "nox", price: 2400},
{name: "ipad", price: 1650},
{name: "xiaomi", price: 2300}]
*/
</script>
后期还会带来更多知识点,喜欢的点赞关注来点糖
边栏推荐
猜你喜欢
随机推荐
如何为项目匹配资源技能和要求?
怒写400篇AI文章!这群妹子卷疯了…
跨境电商看不到另一面:商家刷单、平台封号、黑灰产牟利
亲戚3.5W入职华为后,我也选择了转行……
智能座舱供应链的“新主角”
uniapp引入vantUI库
链表的归并排序[自顶向下分治 || 自低向上合并]
JZ27 二叉树的镜像
Qt读取Json文件(含源码+注释)
NC52 有效括号序列
想要白嫖正则是吧?这一次给你个够!
一文搞懂│php 中的 DI 依赖注入
Mysql 查询语句中where字段= '' 作用是什么 ?如何实现多条件查询
我的创作纪念日
金鱼哥RHCA回忆录:CL210管理计算资源--红帽的超融合基础设施
低光数据集
研发了 5 年的时序数据库,到底要解决什么问题?
Number 类及各子类所占字节数源码分析
word公式复制到另一个word当中出现图片解决方案
几种常见的跨域解决方法