js 数组根据特定规则排序

不说废话直接上代码,自己复制过去看结果 var objs = [ {'name': 'A', 'type': 'fly'}, {'name': 'B', 'type': 'blur'}, {'name': 'C', 'type': 'wipe'}, {'name': 'D', 'type': 'cube'}, {'name': 'E', 'type': 'iris'}, {'name': 'F', 'type': 'fade'} ]; objs.sort(function(a,b){ // order是规则 objs是需要排序的数组 var order = ["wipe", "fly", "iris", "flip", "cube", "blur", "zoom", "fade", "glow", "rotate"]; return order.indexOf(a.type) - order.indexOf(b.type); }); // 根据规则排序后新的数组 var result = objs.map(function(a){ return a['name']; }); alert('Current order is: ' + result.join(', ')); objs 可以不是数组对象,可以直接改成数组
联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2022

备案号:京ICP备15003423号-3