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

JS method to remove duplicate items from array

php中世界最好的语言
Release: 2018-04-14 16:11:04
Original
2503 people have browsed it

This time I will bring you the JS method to remove duplicate items from the array. What are the precautions for JS to remove duplicate items from the array? The following is a practical case. Let’s take a look. one time.

There are many methods to deduplicate 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 of removing duplicate items in an array based on the characteristics of JSobject.

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

Example: Explain the uniqueness of the object key value, that is, when reassigning an existing attribute 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. Analysis of array deduplication steps

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, and use the key of the object as an element in the 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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to use bootstrap responsive navigation bar template

vue.js method to operate array data

The above is the detailed content of JS method to remove duplicate items from 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!