网站首页 > 技术文章 正文
应用场景
在创建排序的栏目名称时,要传入之前排序的最大值+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 工程师。一个会美工和后端的前端架构师。作者,今日头条号/微信公众号:程序员周先生。
- 上一篇: 高阶函数不会用?教你JS中最实用的几个高阶函数用法
- 下一篇: WEB前端面试题
猜你喜欢
- 2024-11-21 浅析GIF 格式图片的存储与解析
- 2024-11-21 如何用2 KB代码实现3D赛车游戏?2kPlus Jam大赛了解一下
- 2024-11-21 快速了解ES6的代理与反射
- 2024-11-21 「实战」蘑菇街 PC 端首页,瀑布流布局的实现原理与细节技巧
- 2024-11-21 Knative 驾驭篇:带你 '纵横驰骋' Knative 自动扩缩容实现
- 2024-11-21 ECMAScript 6使用教程总结
- 2024-11-21 一道二进制子串算法,让面试官都解不出来?
- 2024-11-21 高级前端进阶,为什么要使用call、apply、bind?
- 2024-11-21 碎片时间学编程「202]:分组数组元素
- 2024-11-21 从入门到入土:Lambda完整学习指南,包教包会(上)
- 标签列表
-
- content-disposition (47)
- nth-child (56)
- math.pow (44)
- 原型和原型链 (63)
- canvas mdn (36)
- css @media (49)
- promise mdn (39)
- readasdataurl (52)
- if-modified-since (49)
- css ::after (50)
- border-image-slice (40)
- flex mdn (37)
- .join (41)
- function.apply (60)
- input type number (64)
- weakmap (62)
- js arguments (45)
- js delete方法 (61)
- blob type (44)
- math.max.apply (51)
- js (44)
- firefox 3 (47)
- cssbox-sizing (52)
- js删除 (49)
- js for continue (56)
- 最新留言
-