编程技术文章分享与教程

网站首页 > 技术文章 正文

前端开发编码技巧

hmc789 2024-11-21 15:53:00 技术文章 2 ℃

应用场景

在创建排序的栏目名称时,要传入之前排序的最大值+1。


需求分析

这类题属于常见的:查找对象数组中某属性的最大最小值的快捷方法

例如要查找array数组中对象的columnOrder属性的最大值

var array = [{
	"id": "1280407922569826306",
	"name": "全部",
	"createTime": "2020-07-07 15:46:52",
	"updateTime": "2020-07-07 17:06:05",
	"columnOrder": 1,
	"isFirstScreen": 0,
	"columnLevel": 0,
	"createUserId": "1101",
	"sortTime": 1594108012000,
	"columnIndex": 0
},
{
	"id": "4",
	"name": "有奖专区",
	"createTime": "2020-07-02 16:43:08",
	"updateTime": "2020-07-07 17:06:04",
	"columnOrder": 2,
	"isFirstScreen": 0,
	"columnLevel": 0,
	"createUserId": "",
	"sortTime": 1593679401000,
	"columnIndex": 0
},
{
	"id": "1281470261658357762",
	"name": "栏目一",
	"createTime": "2020-07-10 14:08:14",
	"updateTime": "2020-07-10 14:08:14",
	"columnOrder": 3,
	"isFirstScreen": 1,
	"columnLevel": 1,
	"createUserId": "1101",
	"sortTime": 1594361319779,
	"columnIndex": 0
},
{
	"id": "1281470296336863234",
	"name": "栏目二",
	"createTime": "2020-07-10 14:08:22",
	"updateTime": "2020-07-10 14:08:22",
	"columnOrder": 4,
	"isFirstScreen": 0,
	"columnLevel": 1,
	"createUserId": "1101",
	"sortTime": 1594361307779,
	"columnIndex": 0
},
{
	"id": "1281470321121005569",
	"name": "栏目三",
	"createTime": "2020-07-10 14:08:28",
	"updateTime": "2020-07-10 14:08:28",
	"columnOrder": 5,
	"isFirstScreen": 0,
	"columnLevel": 1,
	"createUserId": "1101",
	"sortTime": 1594361332145,
	"columnIndex": 0
},
{
	"id": "1281470347389931521",
	"name": "栏目四",
	"createTime": "2020-07-10 14:08:34",
	"updateTime": "2020-07-10 14:08:34",
	"columnOrder": 6,
	"isFirstScreen": 0,
	"columnLevel": 1,
	"createUserId": "1101",
	"sortTime": 1594361293600,
	"columnIndex": 0
},
{
	"id": "1281470369447776258",
	"name": "栏目五",
	"createTime": "2020-07-10 14:08:39",
	"updateTime": "2020-07-10 14:08:39",
	"columnOrder": 7,
	"isFirstScreen": 0,
	"columnLevel": 1,
	"createUserId": "1101",
	"sortTime": 1594361301870,
	"columnIndex": 0
},
{
	"id": "1281470394873647105",
	"name": "栏目六",
	"createTime": "2020-07-10 14:08:45",
	"updateTime": "2020-07-10 14:08:45",
	"columnOrder": 8,
	"isFirstScreen": 0,
	"columnLevel": 1,
	"createUserId": "1101",
	"sortTime": 1594361314042,
	"columnIndex": 0
},
{
	"id": "1281470423319416834",
	"name": "栏目七",
	"createTime": "2020-07-10 14:08:52",
	"updateTime": "2020-07-10 14:08:52",
	"columnOrder": 9,
	"isFirstScreen": 0,
	"columnLevel": 1,
	"createUserId": "1101",
	"sortTime": 1594361302042,
	"columnIndex": 0
},
{
	"id": "1282859690692169729",
	"name": "栏目八",
	"createTime": "2020-07-14 10:09:19",
	"updateTime": "2020-07-14 10:09:19",
	"columnOrder": 10,
	"isFirstScreen": 0,
	"columnLevel": 1,
	"createUserId": "1101",
	"sortTime": 1594692559295,
	"columnIndex": 0
}]


代码实现

Math.max.apply(Math, array.map(function(o) {return o.columnOrder}))

同理,查找最小值如下即可:

Math.min.apply(Math, array.map(function(o) {return o.columnOrder}))


问题思考

虽然前端的代码可以实现columnOrder创建使用最大值,从而实现值越大的排序越靠后,但是假如两个管理员同时创建新栏目,客户端传的columnOrder是相同的,那就有并发的问题。所以这题只要了解实现的可行性就好啦,真正的工作还是交给后端实现吧。


关于我的

作者简介:【程序员周先生】,专注完整的 Node.js 技术栈分享,从JavaScript 到 Node.js,再到后端数据库,祝您成为优秀的高级 Node.js 工程师。一个会美工和后端的前端架构师。作者,今日头条号/微信公众号:程序员周先生。

标签列表
最新留言