Home Web Front-end JS Tutorial js implements array deduplication and determines whether the contents in arrays and objects are the same_javascript skills

js implements array deduplication and determines whether the contents in arrays and objects are the same_javascript skills

May 16, 2016 pm 05:11 PM
object Array deduplication

复制代码 代码如下:

/*
*数组元素去重
*/
if(typeof Array.prototype.distinct != "function"){
Array.prototype.distinct = function(){
this.sort();
for(var i=0;iif($.isPlainObject(this[i]) && $.isPlainObject(this[i 1])){
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;iif(typeof a1[i] != typeof a2[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;
}
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Convert an array or object to a JSON string using PHP's json_encode() function Convert an array or object to a JSON string using PHP's json_encode() function Nov 03, 2023 pm 03:30 PM

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

How to implement array deduplication in es5 and es6 How to implement array deduplication in es5 and es6 Jan 16, 2023 pm 05:09 PM

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.

Specify the basis for removing duplicate elements when deduplicating PHP arrays Specify the basis for removing duplicate elements when deduplicating PHP arrays Apr 28, 2024 pm 10:48 PM

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 Use Python's __contains__() function to define the containment operation of an object Aug 22, 2023 pm 04:23 PM

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

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

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.

Source code exploration: How are objects called in Python? Source code exploration: How are objects called in Python? May 11, 2023 am 11:46 AM

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

Use Python's __le__() function to define a less than or equal comparison of two objects Use Python's __le__() function to define a less than or equal comparison of two objects Aug 21, 2023 pm 09:29 PM

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 (&lt;=), Python

How do PHP functions return objects? How do PHP functions return objects? Apr 10, 2024 pm 03:18 PM

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.

See all articles