Home Web Front-end JS Tutorial Filter Custom filter to remove duplicates (with code)

Filter Custom filter to remove duplicates (with code)

Jun 06, 2018 pm 05:47 PM
angularjs filter control Custom filters

This time I bring you Filter custom filter deduplication (with code). What are the precautions for Filter custom filter deduplication? Here is a practical case, let’s take a look.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title> ng-repeat去除重复</title>

<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>

</head>

<body>

<p ng-app="myApp" ng-controller="myCtrl">

  <p ng-repeat="x in items | unique:&#39;id&#39;">

    {{x.id}}---{{x.name}}

  </p>

</p>

<script>

  //AngularJs 自定义过滤器

  //1.使用过滤器,去除重复

  angular.module('common', []).filter('unique'function () {

    return function (collection, keyname) {

      console.info(collection);

      console.info(keyname);

      var output = [],

        keys = [];

      angular.forEach(collection, function (item) {

        var key = item[keyname];

        if (keys.indexOf(key) === -1) {

          keys.push(key);

          output.push(item);

        }

      });

      return output;

    }

  });

  var app = angular.module('myApp', ['common']);

  app.controller('myCtrl'function ($scope) {

    //$scope.items = [1, 2, 3,2];

    //当前unique 的过滤只针对,对象数组过滤

    $scope.items = [

      { id: 1, name: 'zhangsan' },

      { id: 2, name: 'lisi' },

      { id: 1, name: 'zhangsan' },

    ];

  });

</script>

</body>

</html>

Copy after login

Run results:

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:

highslide.js plug-in project practice (with code)

JS operation page background darkening

The above is the detailed content of Filter Custom filter to remove duplicates (with code). For more information, please follow other related articles on the PHP Chinese website!

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 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)

Using PHP to control the camera: analysis of the entire process from connection to shooting Using PHP to control the camera: analysis of the entire process from connection to shooting Jul 30, 2023 pm 03:21 PM

Use PHP to control the camera: Analyze the entire process from connection to shooting. Camera applications are becoming more and more widespread, such as video calls, surveillance systems, etc. In web applications, we often need to control and operate cameras through PHP. This article will introduce how to use PHP to realize the entire process from camera connection to shooting. Confirm the connection status of the camera. Before starting to operate the camera, we first need to confirm the connection status of the camera. PHP provides an extension library video to operate the camera. We can pass the following code

How to disable media volume control popups [permanently] How to disable media volume control popups [permanently] May 24, 2023 pm 10:50 PM

When you use the corresponding shortcut key to fine-tune the volume level, a media volume control pop-up will appear on the screen. This can be annoying, so read on to find out different ways to permanently disable media volume control pop-ups. How to disable media volume control popup? 1. Click the Windows icon on the taskbar in Google Chrome, type chrome in the search bar at the top, and select the relevant search results to launch Google Chrome. Type or copy-paste the following into the address bar and press the key. Enterchrome://flags type media keys in the search box at the top and select Disable in the Hardware Media Key Handling drop-down list. Now exit the Google Chrome app and relaunch it. Google

How to implement robot control using Java How to implement robot control using Java Jun 16, 2023 am 10:36 AM

In recent years, robotic technology has been widely used, which shows its importance in the field of science and technology. Robot control is also one of the core parts of robot development. Using Java language to realize robot control can achieve fast robot control and provide strong support for the further development of robots. Java is a high-level language that has become a widely used programming language due to its good cross-platform, efficiency and security. It also provides good support in implementing robot control. First of all, you need to understand the robot control

How to solve the '[Vue warn]: Failed to resolve filter' error How to solve the '[Vue warn]: Failed to resolve filter' error Aug 19, 2023 pm 03:33 PM

Methods to solve the "[Vuewarn]:Failedtoresolvefilter" error During the development process using Vue, we sometimes encounter an error message: "[Vuewarn]:Failedtoresolvefilter". This error message usually occurs when we use an undefined filter in the template. This article explains how to resolve this error and gives corresponding code examples. When we are in Vue

The latest 5 angularjs tutorials in 2022, from entry to mastery The latest 5 angularjs tutorials in 2022, from entry to mastery Jun 15, 2017 pm 05:50 PM

Javascript is a very unique language. It is unique in terms of the organization of the code, the programming paradigm of the code, and the object-oriented theory. The issue of whether Javascript is an object-oriented language that has been debated for a long time has obviously been There is an answer. However, even though Javascript has been dominant for twenty years, if you want to understand popular frameworks such as jQuery, Angularjs, and even React, just watch the "Black Horse Cloud Classroom JavaScript Advanced Framework Design Video Tutorial".

How to implement API versioning in FastAPI How to implement API versioning in FastAPI Jul 29, 2023 am 11:33 AM

How to implement API version control in FastAPI Introduction: With the rapid development of software development, API version control has become more and more important. As our applications continue to evolve and improve, we often need to make updates and modifications to the API. This requires us to be able to smoothly introduce new API versions without affecting the old versions. In this article, we will discuss how to implement API versioning in FastAPI. FastAPI is a modern web framework based on Python that provides fast

Control Center not working in iPhone: Fix Control Center not working in iPhone: Fix Apr 17, 2024 am 08:16 AM

Imagine an iPhone without a functioning Control Center. You can't, right? If the buttons on the Control Center don't work properly, you won't be able to use your iPhone properly. The main idea of ​​Control Center is to easily access certain features directly from anywhere on your phone. In this case, these solutions will help to resolve the issue on your phone. Fix 1 – Use a Cloth to Clean Your Phone Sometimes the upper part of the display gets dirty from regular use. This may cause the Control Center to not function properly. Step 1 – Take a soft, clean microfiber cloth and clean the top half of your iPhone screen. You can also use any screen cleaning solution. Step 2 – Make sure to remove any dust, oil, or anything else from your phone’s display. After clearing phone screen

Use PHP and AngularJS to build a responsive website to provide a high-quality user experience Use PHP and AngularJS to build a responsive website to provide a high-quality user experience Jun 27, 2023 pm 07:37 PM

In today's information age, websites have become an important tool for people to obtain information and communicate. A responsive website can adapt to various devices and provide users with a high-quality experience, which has become a hot spot in modern website development. This article will introduce how to use PHP and AngularJS to build a responsive website to provide a high-quality user experience. Introduction to PHP PHP is an open source server-side programming language ideal for web development. PHP has many advantages, such as easy to learn, cross-platform, rich tool library, development efficiency

See all articles