javascript 求最大前5个数; 对象 深拷贝 deep copy
* 用数组
function getTopN(a, n) { function _cloneArray(aa) { var n = aa.length, a = new Array(n); for (var i = 0; i < n; i++) { a[i] = aa[i]; } return a; } function _getMaxElem(a) { if (a.length === 0) throw "empty array"; if (a.length === 1) return { index: 0, value: a[0] }; var max = a[0], index = 0; for (var i = 0; i < a.length; i++) { if (a[i] > max) { max = a[i]; index = i; } } return { index: index, value: max } } var o = {}, b = [], c = _cloneArray(a);; while (n--) { o = _getMaxElem(c); b.push(o.value); c.splice(o.index, 1); } return b; }
// test
/* var a= []; for (var i = 0; i < 12; i++) { a.push(Math.round(Math.random()*100)); } console.log(a); */ var a = [3, 12, 17, 16, 73, 32, 61, 46, 52, 49, 6, 5]; var b = getTopN(a, 5); console.log('max elements: [' + b.toString() + ']'); // [73,61,52,49,46] console.log('-------------sorted------------------'); console.log(a.sort(function(a, b) { return b - a; }));
* 用链表 []
+ splice方法 点击查看我自己的博客 https://www.cnblogs.com/mingzhanghui/p/9317179.html
function getTopN(a, n) { function _getMaxElem(a) { if (a.length === 0) throw "empty array"; if (a.length === 1) return { index: 0, value: a[0] }; var max = a.get(0), index = 0, c; for (var i = 0; i < a.length; i++) { c = a.get(i); if (c > max) { max = c; index = i; } } return { index: index, value: max } } var o = {}, b = []; while (n--) { o = _getMaxElem(a); b.push(o.value); a.splice(o.index, 1); } return b; } /** * max elements: [73,61,52,49,46] * -------------sorted------------------ * [ 73, 61, 52, 49, 46, 32, 17, 16, 12, 6, 5, 3 ] */ var a = [3, 12, 17, 16, 73, 32, 61, 46, 52, 49, 6, 5]; var list = new LinkedList(a); var top5 = getTopN(list, 5); console.log('max elements: [' + top5.toString() + ']'); console.log('-------------sorted------------------'); console.log(a.sort(function(a, b) { return b - a; }));
-------------------------------------------------------------------
clone object:
function clone(obj) { // Handle the 3 simple types, and null or undefined if (null == obj || "object" != typeof obj) return obj; var copy; // Handle Date if (obj instanceof Date) { copy = new Date(); copy.setTime(obj.getTime()); return copy; } // Handle Array if (obj instanceof Array) { var n = obj.length; copy = new Array(n); for (var i = 0; i < n; i++) { copy[i] = obj[i]; } return copy; } // Handle Object if (obj instanceof Object) { copy = {}; for (var attr in obj) { if (obj.hasOwnProperty(attr)) { copy[attr] = clone(obj[attr]); // 双向循环列表会无限递归🤔 stackoverflow } } return copy; } throw new Error("Unsupported type for copying object..."); }
// test:
// test var o = { name1:[1,2,3,4], name2:{ inner1:[1,2], inner2: null, inner3:{a:[1,2], b:"this is good", c:3, d:null, e:undefined}} }; var p = clone(o); console.log(o===p); // false console.log(o.toString()===p.toString()); // true console.log('----------'); var date1 = new Date(); var date2 = clone(date1); var date3 = date1; console.log(date2===date1); // false; console.log(date2.getTime()===date1.getTime()); // true console.log(date3===date1); // true;