


js implements array deduplication and determines whether the contents in arrays and objects are the same_javascript skills
/*
*数组元素去重
*/
if(typeof Array.prototype.distinct != "function"){
Array.prototype.distinct = function(){
this.sort();
for(var i=0;i
if(o2o(this[i],this[i 1])){
this.splice(i,1);
}
}else if($.isArray(this[i]) && $.isArray(this[i 1])){
if(a2a(this[i],this[i 1])){
this.splice(i,1);
}
}else if(this[i]===this[i 1]){
this.splice(i,1);
}
}
}
}
/*
*比较对象是否相同
*/
function o2o(o1,o2){
if(!($.isPlainObject(o1) && $.isPlainObject(o2))){
return false;
}
var k1k2=[],k1 =[],k2=[];
$.each(o1,function(k,v){
k1.push(k);
});
$.each(o2,function(k,v){
k2.push(k);
});
if(k1.length != k2.length){
return false;
}
k1k2 = k1;
k1k2 = k1k2.concat(k2);
k1k2.distinct();
if(k1.length != k1k2.length || k2.length != k1k2.length){
return false;
}
var flag=true;
$.each(k1k2,function(i,v){
var v1= o1[v];
var v2 =o2[v];
if(typeof v1 != typeof v2){
flag= false;
}else{
if($.isPlainObject(v1) && $.isPlainObject(v2)){//recursion
flag = o2o(v1,v2);
if(!flag){
return false;
}
}else if($.isArray(v1) && $.isArray(v2)){
flag = a2a(v1,v2);
if(!flag){
return false;
}
}else{
if(v1 !== v2){
flag= false;
}
}
}
});
return flag;
}
/*
*比较数组是否完全相同
*/
function a2a(a1,a2){
if(!($.isArray(a1) && $.isArray(a2))){
return false;
}
if(a1.length != a2.length){
return false;
}
a1.sort();
a2.sort();
for(var i=0;i
return false;
}
if($.isPlainObject(a1[i]) && $.isPlainObject(a2[i])){
var retVal = o2o(a1[i],a2[i]);
if(!retVal){
return false;
}
}else if($.isArray(a1[i]) && $.isArray(a2[i]) ){//recursion
if(!a2a(a1[i],a2[i])){
return false;
}
}else if(a1[i] !== a2[i]){
return false;
}
}
return true;
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

In es5, you can use the for statement and indexOf() function to achieve array deduplication. The syntax "for(i=0;i<array length;i++){a=newArr.indexOf(arr[i]);if(a== -1){...}}". In es6, you can use the spread operator, Array.from() and Set to remove duplication; you need to first convert the array into a Set object to remove duplication, and then use the spread operator or the Array.from() function to convert the Set object back to an array. Just group.

PHP's array_unique() function is used to remove duplicate elements from an array. By default, strict equality (===) is used. We can specify the basis for deduplication through a custom comparison function: create a custom comparison function and specify the deduplication standard (for example, based on element length); pass the custom comparison function as the third parameter to the array_unique() function. Remove duplicate elements based on specified criteria.

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Wedge We know that objects are created in two main ways, one is through Python/CAPI, and the other is by calling a type object. For instance objects of built-in types, both methods are supported. For example, lists can be created through [] or list(). The former is Python/CAPI and the latter is a calling type object. But for instance objects of custom classes, we can only create them by calling type objects. If an object can be called, then the object is callable, otherwise it is not callable. Determining whether an object is callable depends on whether a method is defined in its corresponding type object. like

Title: Using Python's __le__() function to define a less than or equal comparison of two objects In Python, we can define comparison operations between objects by using special methods. One of them is the __le__() function, which is used to define less than or equal comparisons. The __le__() function is a magic method in Python and is a special function used to implement the "less than or equal" operation. When we compare two objects using the less than or equal operator (<=), Python

PHP functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.
