mindoc/static/js/array.js

85 lines
2.0 KiB
Go
Raw Normal View History

/**
*
* @param $callback
*/
Array.prototype.remove = function ($callback) {
var $isFunction = typeof $callback === "function";
var arr = [];
2018-11-21 18:07:24 +08:00
for (var $i = 0, $len = this.length; $i < $len; $i++) {
if ($isFunction) {
if ($callback(this[$i])) {
arr.push($i);
}
2018-11-21 18:07:24 +08:00
} else if (this[$i] == $callback) {
arr.push($i);
}
}
2018-11-21 18:07:24 +08:00
for ($i = 0, $len = arr.length; $i < $len; $i++) {
this.slice($i, 1);
}
};
2018-11-21 18:07:24 +08:00
String.prototype.endWith = function (endStr) {
var d = this.length - endStr.length;
return (d >= 0 && this.lastIndexOf(endStr) === d)
};
//格式化文件大小
function formatBytes($size) {
if (typeof $size === "number") {
var $units = [" B", " KB", " MB", " GB", " TB"];
for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
return $size.toFixed(2) + $units[$i];
}
return $size;
}
2018-11-21 18:07:24 +08:00
/**
* jsonjson
* @param $json
* @param $parentKey
*/
function foreachJson($json, $parentKey) {
var data = {};
$.each($json, function (key, item) {
var cKey = $parentKey;
if (Array.isArray($json)) {
key = "[";
}
if ($parentKey !== undefined && $parentKey !== "" && key !== "") {
if($parentKey.endsWith("[")) {
cKey = $parentKey + key + "]";
} else if (key === "[") {
cKey = $parentKey + key;
} else {
cKey = $parentKey + "." + key;
}
} else {
cKey = key;
}
var node = {};
node["key"] = cKey;
node["type"] = Array.isArray(item) ? "array" : typeof item;
node["value"] = item;
if (typeof key === "string" && key !== "[") {
data[cKey] = node;
}
if (typeof item === "object") {
var items = foreachJson(item, cKey);
$.each(items,function (k,v) {
data[k] = v;
});
}
});
return data;
}