当前位置:网站首页>js通过两种方式进行对商品价格排序
js通过两种方式进行对商品价格排序
2022-08-02 16:12:00 【编程小橙子】

javascript-illustration.png
js通过两种方式进行对商品价格排序
<template>
<div class="content"></div>
</template>
<script>
export default {
data() {
return {
user: {
order_list: [
{ id: 1, title: "java", click: 300, price: 600 },
{ id: 2, title: "react", click: 240, price: 460 },
{ id: 3, title: "vue", click: 506, price: 820 },
{ id: 4, title: "c#", click: 157, price: 765 },
{ id: 5, title: "php", click: 650, price: 100 },
{ id: 6, title: "c语言", click: 80, price: 920 },
],
//方式二
userOrderSort() {
this.order_list.reduce((pre, cur) => {
return pre.price > cur.price ? 1 : -1;
}, []);
},
},
};
},
mounted() {
this.orderSort();
this.user.userOrderSort();
},
methods: {
//方式一
orderSort() {
this.user.order_list.sort(order("price"));
},
},
};
// 封装排序方法
function order(filed, type = "asc") {
return (a, b) => {
if (type == "asc") return a[filed] > b[filed] ? 1 : -1;
return a[filed] > b[filed] ? -1 : 1;
};
}
</script>
<style lang="scss"></style>打印出来的结果
[
{
"id": 5,
"title": "php",
"click": 650,
"price": 100
},
{
"id": 2,
"title": "react",
"click": 240,
"price": 460
},
{
"id": 1,
"title": "java",
"click": 300,
"price": 600
},
{
"id": 4,
"title": "c#",
"click": 157,
"price": 765
},
{
"id": 3,
"title": "vue",
"click": 506,
"price": 820
},
{
"id": 6,
"title": "c语言",
"click": 80,
"price": 920
}
]后期还会带来更多知识点,喜欢的点赞关注来点糖
边栏推荐
猜你喜欢
随机推荐
29. 两数相除
莫比乌斯反演学习笔记
默认用户名和密码(SQL)
julia系列3:函数、模块与宏
代码随想录笔记_哈希_61扑克牌中的顺子
【[NOI2001] 炮兵阵地】【状压DP】
CWE4.8:2022年危害最大的25种软件安全问题
Redis进阶之路:深度解析Redis单线程架构,图文并茂不能再清晰了
JZ27 二叉树的镜像
机械臂速成小指南(十七):直线规划
Mysql 查询语句中where字段= '' 作用是什么 ?如何实现多条件查询
“如何写好一篇学术论文?”这大概是最详实的一则攻略了!
【Codeforces Round #811 (Div. 3)】【题目解析+AK代码】
JZ70 矩形覆盖
【wpf】ListView 和 ItemsControl 的一点区别
领导无线边缘AI的联合神经形态学习,具有较高的识别精度以及较低的能耗
面试官:可以谈谈乐观锁和悲观锁吗
从幻核疑似裁撤看如何保证NFT的安全
总结嵌入式C语言难点 (1部分) 【结尾有资料】
QueryWrapper方法解释









