js基础4 — 数组操作
1. 数据基本操作方法
<script type="text/javascript"> "use strict"; var console = window.console; var a = ['a', 'b', 'c']; var b = ['x', 'y', 'z']; var c = a.concat(b, false); console.log('1.' + a); // ['a','b','c']; console.log('1.' + c); // ['a','b','c','x','y','z',false]; var a = ['a','b','c']; var b = ['x','y','z']; var c = a.push(b, true); console.log('2.' + a); // ['a','b','c',['x','y','z'],true]; console.log('2.' + c); // 5 返回数组的长度 //扩展: //若要把两个两个数组合并,使用apply()方法; // var d = a.push.apply(a,b); // var e = a.push(b); // console.log(a); // console.log(e); var n = ['123', '23', '32', 'a', 'aa']; n.sort(); console.log('3.' + n); var n = ['123', '23', '32', 'a', 'aa']; n.sort(function(a,b) { return a-b; // }); console.log('4.' + n); //["23", "32", "123", "a", "aa"] // 扩展: var scores = [1, 10, 21, 2]; scores.sort(); //console.log(scores); // [1, 10, 2, 21] // 注意10在2之前, // 因为在 Unicode 指针顺序中"10"在"2"之前 var things = ['word', 'Word', '1 Word', '1 Word3', '2 Words']; things.sort(); //console.log(things); // ['1 Word', '2 Words', 'Word', 'word'] // 在Unicode中, 数字在大写字母之前, // 大写字母在小写字母之前.字母短的在字母串长的前面 var y = [5,8,3,10]; var f = y.some(function(v){ return v>7; }); console.log('5.' + f); //ture var x = [5,8,3,10]; var h = x.every(function(v){ return v>7; }); console.log('6.' + h);
</script>
2. 生成0~100的随机数组 / 计算数组中距离最大的两组坐标
<script type="text/javascript"> (function () { "use strict"; var console = window.console; function random(a,b) { return Math.random() > 0.5 ? -1 : 1; } var arr = []; for (var i=0; i<99; i++) { var num = Math.random()*100; arr[i] = num; arr.push(arr[i]); } console.log('1.生成0~100随机排序的数组:'); console.log(arr.sort(random())); var arr2 = [],i=0; while(i<10) { var index = Math.floor(Math.random()*100); if (arr2.indexOf(arr[index]) == -1) { arr2[i] = arr[index]; i++; } else { } } console.log('arr2为:'); console.log(arr2); // var arr2 = [1,2,3,4,5,6,7,8,9,10]; var arr3 = []; var z = [],i; for (var i=0; i<9; i++) { if(i%2 === 0) { arr3[i/2] = { x: arr2[i], y: arr2[i+1] }; z[i/2] = Math.abs(arr3[i/2].y * arr3[i/2].y - arr3[i/2].x * arr3[i/2].x); } } console.log('arr3为:'); console.log(arr3); var max = Math.max.apply(null,z); var min = Math.min.apply(null,z); console.log('arr3中距离最大的两组坐标为:'); for (var i=0; i<z.length; i++) { if (z[i] === max) { console.log(arr3[i]); } else if (z[i] === min) { console.log(arr3[i]); } } })(); </script>
3. 数组去重 / 将对象转为指定的字符串形式
<script type="text/javascript"> "use strict"; var console = window.console; var $ = window.$; var arr = [1,2,3,'1','2','3','true',true,3,2,1]; Array.prototype.filter = function() { var arr2 = []; for (var i=0; i<arr.length; i++) { if (arr2.indexOf(arr[i]) == -1) { arr2.push(arr[i]); } } return arr2; }; console.log('去重后为:'); console.log(arr.filter()); var obj = { w: 'width', h: 'height', di: 'display:none', c: 'color' }; var funRplByKey = function(str) { var arr = []; var arr2 = []; for( var i in obj ) { arr.push(obj[i]); arr2.push(i); } for (var i=0; i<arr.length; i++) { if (str.indexOf(arr[i]) !== -1) { str = str.replace(arr[i],arr2[i]); } } return str; }; var str = '.class{width:20px;height:10px;display:none;}'; console.log(funRplByKey(str)); </script>