Home > Web Front-end > JS Tutorial > body text

JS implements the function of removing duplicate items in an array

小云云
Release: 2018-01-18 13:22:52
Original
1666 people have browsed it

This article mainly introduces the object-based features of JS to achieve the function of removing duplicate items in the array. It analyzes in detail the specific steps and related operation skills of JS to implement array deduplication based on the uniqueness of key values ​​in the form of examples. Friends who need it can For reference, I hope it can help everyone.

The example in this article describes the object-based feature of JS to achieve the function of removing duplicate items in an array. I would like to share it with you for your reference. The details are as follows:

There are many methods for deduplicating arrays, and different methods have different efficiencies. For example, in the previous article, the summary of the array deduplication algorithm implemented by JS summarized and analyzed 4 implementation methods. Here we introduce an efficient array deduplication method: a method to remove duplicate items in an array based on the characteristics of JS objects.

1. Characteristics of JS objects (characteristics used in this article): key is always unique

Example: Explain the uniqueness of object key value property, that is, when reassigning an existing property in js, the key is actually overwritten instead of creating a new key


var t={name:'张三',age:20};//定义个js对象
console.log(t.name);//控制台输出:张三
//注意:此时对象t有两个属性:name、age
t.name='李四';
console.log(t.name);//控制台输出:李四
//注意:此时对象t依然有两个属性:name、age
Copy after login

2. Array deduplication step analysis

is divided into two steps:

1. Convert the deduplicated array into a js object and return it. Conversion rules: change the value in the array into the key in the js object, and then give the value to any value;

2. Restore the object in step 1 to an array, the key of the object as an element in an array.

3. Array deduplication implementation


var arr=[1,2,3,4,5,23,4,2,4,3];
//1.把数组装换成对象,数组的元素作为对象的key,然后返回对象
function toObject(ac_array){
    var obj={};//私有的对象
    for (var i=0;i<ac_array.length;i++) {
      obj[ac_array[i]] = true;
    }
    console.log(obj);//Object {1: true, 2: true, 3: true, 4: true, 5: true, 23: true}
    return obj;
}
//2.把对象的key获取出来作为数组的元素,然后返回数组
function keys(ac_obj){
    var arr = [];
    for(var item in ac_obj){
      if(ac_obj.hasOwnProperty(item)){
        arr.push(item);
      }
    }
    console.log(arr);//["1", "2", "3", "4", "5", "23"]
    return arr;
}
//综合
function uniq(ac_array){
    return keys(toObject(ac_array));
}
//测试
var uniq_array=uniq(arr);
console.log(uniq_array);//["1", "2", "3", "4", "5", "23"]
Copy after login

Code test results:

Related recommendations:

How to remove duplicates from a two-dimensional array in PHP

java data structure and algorithm Example of noDups algorithm to remove duplicates

PHP two-dimensional array custom function to remove duplicates_PHP tutorial

The above is the detailed content of JS implements the function of removing duplicate items in an array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!