2015-1-30新增:
javascript常用方法函数收集 http://www.css88.com/archives/5180
一些通用的javascript方法:
function range(start, end) {
var result = [];
if(typeof end === 'undefined') {
end = start;
start = 0;
}
for(;start < end; start++) {
result.push(start);
}
return result;
}
undefined
range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(4)
[0, 1, 2, 3]
range(2)
[0, 1]
一个array的shuffle打乱函数
Array.prototype.randomEach = function(func) {
if (typeof func != "function") {
throw new TypeError();
}
var len = this.length,
indexes = range(len);
while(len) {
var cursor = Math.floor(Math.random() * (len--));
if(func(this[indexes[cursor]]) === false) {
break;
}
}
};
对比下常规的shuffle函数
function shuffle(array) {
var currentIndex = array.length
, temporaryValue
, randomIndex
;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var arr = [2, 11, 37, 42];
shuffle(arr);
console.log(arr);shuffle(arr);
console.log(arr);shuffle(arr);
console.log(arr);
[42, 2, 11, 37, confound: function]
[2, 11, 37, 42, confound: function]
[2, 37, 11, 42, confound: function]
下面还是一个shuffle函数
Array.prototype.confound = function () {
this.sort(function () {
return Math.random() - 0.5;
});
};
a
["c", "b", "a"]
a.confound();
undefined
a
["b", "a", "c"]
下面还是一个shuffle函数
Array.prototype.confound = function () {
this.sort(function () {
return Math.random() - 0.5;
});
};
a
["c", "b", "a"]
a.confound();
undefined
a
["b", "a", "c"]
克隆object的方法
function _shadowClone(obj){
var result = {};
for(var key in obj){
if(obj.hasOwnProperty(key)){
result[key] = obj[key];
}
}
return result;
}
Json---》Array
function json2array(json){
var result = [];
var keys = Object.keys(json);
keys.forEach(function(key){
result.push(json[key]);
});
return result;
}
//example
json2array({a:1,b:2});
[1, 2]
//more complex example
json2array({20131013: 66, 20131014: 198, 20131015: 253, 20131016: 352, 20131017: 293, 20131018: 277, 20131019: 111, 20131020: 91, 20131021: 255, 20131022: 256, 20131023: 293, 20131024: 390, 20131025: 401, 20131026: 117, 20131027: 45, 20131028: 54, 20131029: 59, 20131030: 66, 20131031: 109, 20131101: 32});
[66, 198, 253, 352, 293, 277, 111, 91, 255, 256, 293, 390, 401, 117, 45, 54, 59, 66, 109, 32]
Array---》String
var a = [{"obj1":"phpgcs"}, {"obj2":"ganiks"}]
console.log(a);
//[Object, Object]
JSON.stringify(a)
//"[{"obj1":"phpgcs"},{"obj2":"ganiks"}]"
String---》Array
"1,2".split(",").map(Number);
[1, 2]
"1,2".split(",");
["1", "2"]
JSON.parse("["+"1,2"+"]");
[1, 2]
Object---》Json
//php print_r($response);
.post(url, params, function(data){
console.log(data);//data并不是规范的JSON格式的
data = $.parseJSON(data);
console.log(data);
});<br>//这里如果直接用.getJSON方法获取ajax数据则直接就是规范的JSON数据
Array---》Object
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i)
rv[i] = arr[i];
return rv;
}
Your array already is more-or-less just an object, but arrays do have some "interesting" and special behavior with respect to integer-named properties. The above will give you a plain object.
edit oh also you might want to account for "holes" in the array:
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i)
if (arr[i] !== undefined) rv[i] = arr[i];
return rv;
}
参考:
http://phpgcs.com/2013/10/31/javascript-tricks-collection.html
http://stackoverflow.com/questions/17684921/sort-json-object-in-javascript
http://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value
http://stackoverflow.com/questions/4215737/convert-array-to-object