当前位置:网站首页>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
}
]
后期还会带来更多知识点,喜欢的点赞关注来点糖
边栏推荐
猜你喜欢
随机推荐
一文搞懂│php 中的 DI 依赖注入
2.5 - 死锁
A tour of gRPC: 06 - gRPC client straming
JZ4 二维数组中的查找
2.NVIDIA Deepstream开发指南中文版--自述文件
无线振弦采集仪远程修改参数方式
[LeetCode]剑指 Offer 54. 二叉搜索树的第k大节点
“如何写好一篇学术论文?”这大概是最详实的一则攻略了!
【学术相关】北大一研究生偷取同门师妹论文发表,作者却挂名给了武大 3 位不知情的学生......
NC22 合并两个有序的数组
打破千篇一律,DIY属于自己独一无二的商城
【Transformer专题】Vision Transformer(ViT)原理 + 代码
JZ10 斐波那契数列
品牌方发行NFT时,应如何考量实用性?
怒写400篇AI文章!这群妹子卷疯了…
面试官:可以谈谈乐观锁和悲观锁吗
JZ27 二叉树的镜像
JZ21 调整数组顺序使奇数位于偶数前面(一)-相对位置变化
DSP-ADAU1452参考音设置方法
尚硅谷尚品项目汇笔记(二)