/**
* Compare two variables is the same
* @param {Object} fobj
* @param {Object} sobj
* @return (Boolean)
*/
var compare = function (Fobj, sobj) {
var ftype = typeof (Fobj);
var stype = typeof (Sobj);
if (ftype = = stype) {
if (Ftype = = "Object") {
if (Fobj.constructor = = Array && sobj.constructor = = array) {
Return Comparearray (Fobj, Sobj)
else if (fobj.constructor!= array && sobj.constructor!= array) {
Return Compareobject (Fobj, sobj);
}
return false;
}
return fobj = = Sobj;
}
return false;
}
/**
* Compare whether two objects are the same
* @param {Object} fobj
* @param {Object} sobj
* @return (Boolean)
*/
var compareobject = function (Fobj, sobj) {
for (var ele in fobj) {
if (sobj[ele] = = undefined) {
return false;
}
if (!compare (Fobj[ele], Sobj[ele])) {
return false;
}
}
for (var ele in sobj) {
if (fobj[ele] = = undefined) {
return false;
}
if (!compare (Fobj[ele], Sobj[ele])) {
return false;
}
}
return true;
}
/**
* Comparison of two arrays is the same
* @param {Object} farr
* @param {Object} Sarr
* @return (Boolean)
*/
var Comparearray = function (Farr, Sarr) {
if (farr.length!= sarr.length) {
return false;
}
for (var i = 0; i < farr.length; i++) {
if (!compare (Farr[i], sarr[i])) {
return false;
}
}
return true;
}
/**
* Calculate Object size
* @param {Object} obj
* @author Shenkong (shenkong@php.net)
*/
var count = function (obj) {
var length = 0;
if (typeof obj = = ' object ') {
for (k in obj) {
length++;
}
}
return length;
}
/**
* Check whether there is a value in the array
* Search for needle in haystack, return TRUE if found, or FALSE.
* @param {Object} needle
* @param {Object} haystack
* @author Shenkong (shenkong@php.net)
*/
var in_obj = function (needle, haystack) {
For (key in haystack) {
if (compare (needle, Haystack[key])) {
return true;
}
}
return false;
}
/**
* Check if the given key name or index exists in the array
* @param {Object} key
* @param {Object} search
* @author Shenkong (shenkong@php.net)
*/
var obj_key_exists = function (key, search) {
if (typeof key!= ' number ' && typeof key!= ' string ') {
return false;
}
For (k in search) {
if (k = = key) {
return true;
}
}
return false;
}
/**
* Return all the values in input and establish a digital index for them
* @param {Object} input
* @author Shenkong (shenkong@php.net)
*/
var obj_values = function (input) {
var o = [];
For (key in input) {
O.push (Input[key]);
}
return o;
}
/**
* Returns all the key names in the array
* Returns the number of the input array or the key name of the string.
* If an optional parameter search_value is specified, only the key name of the value is returned. Otherwise, all key names in the input array will be returned.
* @param {Object} input
* @param {Object} search_value
* @author Shenkong (shenkong@php.net)
*/
var Obj_keys = function (input, search_value) {
if (typeof input!= ' object ') {
return false;
}
var o = [];
if (Search_value = = undefined) {
For (key in input) {
O.push (key);
}
} else {
For (key in input) {
if (compare (Input[key], search_value)) {
O.push (key);
}
}
}
return o;
}
/**
* Create an array with the value of one array as its key name and the value of the other array as its value.
* Returns FALSE if the number of cells in the two arrays is different or the array is empty.
* If the keys array value has a non string or numeric return FALSE.
* @param {Object} keys
* @param {Object} values
* @author Shenkong (shenkong@php.net)
*/
var obj_combine = function (keys, values) {
if (keys.length!= values.length) {
return false;
}
if (Keys.length = = 0) {
return false;
}
if (typeof keys!= ' object ' | | typeof values!= ' object ') {
return false;
}
var o = {};
for (var i = 0; i < keys.length; i++) {
if (typeof Keys[i]!= ' number ' && typeof keys[i]!= ' string ') {
return false;
}
O[keys[i]] = values[i];
}
return o;
}
/**
* Search for the given value in the array, and return the corresponding key name if successful
* Search for needle parameter in haystack and return the key name if found, otherwise return FALSE.
* If needle occurs more than once in haystack, the first matching key is returned.
* The key to return all matching values should be replaced with Obj_keys () plus optional parameter search_value.
* @param {Object} needle
* @param {Object} haystack
* @author Shenkong (shenkong@php.net)
*/
var obj_search = function (needle, haystack) {
if (typeof haystack!= ' object ') {
return false;
}
For (key in haystack) {
if (compare (Haystack[key], needle)) {
Return key;
}
}
return false;
}
/**
* Count the number of occurrences of the values in the array
* Returns an object that uses the value in the input array as the key name, and the value that appears in the input array as a value.
* @param {Object} input
* @author Shenkong (shenkong@php.net)
*/
var obj_count_values = function (input) {
var o = {};
For (key in input) {
if (O[input[key]] = = undefined) {
O[input[key]] = 1;
} else {
o[input[key]]++;
}
}
return o;
}
/**
* Exchange the keys and values in the object
* Returns an inverted object, for example, the key name in trans becomes a value, and the value in trans is the key name.
* If the same value occurs more than once, the last key will be its value, and all the others will be lost.
* @param {Object} trans
* @author Shenkong (shenkong@php.net)
*/
var obj_flip = function (trans) {
var o = {};
For (key in trans) {
O[trans[key]] = key;
}
return o;
}
/**
* Returns the last cell of the object
* @param {Object} obj
* @author Shenkong (shenkong@php.net)
*/
var end = function (obj) {
for (key in obj) {
}
return Obj[key];
}
/**
* Function The callback function on the cell of the given array
* Returns an object that contains all the cells in the arr1 after the callback function.
* The number of parameters accepted by callback should be the same as the number of arrays passed to the Obj_map () function.
* @param {Object} callback
* @author Shenkong (shenkong@php.net)
*/
var obj_map = function (callback, ARR1) {
var o = [];
if (callback = = null) {
callback = function () {
var a = [];
for (var i = 0; i < arguments.length; i++) {
A.push (Arguments[i]);
}
return A;
}
}
for (var i = 1; i < arguments.length; i++) {
Arguments[i] = obj_values (Arguments[i]);
}
var length = 0;
for (var i = 1; i < arguments.length; i++) {
if (Arguments[i].length > Length) {
length = Arguments[i].length;
}
}
for (var i = 0; i < length; i++) {
var cmd = [];
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.