Home Web Front-end JS Tutorial Share an example code that uses js to filter repeated elements in an array

Share an example code that uses js to filter repeated elements in an array

May 05, 2017 am 10:18 AM

This article mainly introduces javascriptFilteringArrayRelated information on the implementation method of repeated elements. Friends in need can refer to

javascript filtering Implementation method of repeated elements in array

The following is the information found on the Internet, which can be used directly in the project. You can refer to it:

Implementation code:

function filterArray(receiveArray){
var arrResult = new Array(); //定义一个返回结果数组.
	for (var i=0; i<receiveArray.length; ++i) { 
		if(check(arrResult,receiveArray[i]) == -1) {
			//在这里做i元素与所有判断相同与否
			arrResult.push(receiveArray[i]); 
			// 添加该元素到新数组。如果if内判断为false(即已添加过),
			//则不添加。
		}
	}
	return arrResult;
}
function check(receiveArray,checkItem){
	var index = -1; // 函数返回值用于布尔判断
	for(var i=0; i<receiveArray.length; ++i){
		if(receiveArray[i]==checkItem){
			index = i;
			break;
			}
		}
	return index;
}
Copy after login

[Related recommendations]

1. Free js online video tutorial

2. JavaScript Chinese Reference Manual

3. php.cn Dugu Jiujian (3) - JavaScript video tutorial

The above is the detailed content of Share an example code that uses js to filter repeated elements in an array. 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 Article Tags

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)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

How to remove duplicate elements from PHP array using foreach loop? How to remove duplicate elements from PHP array using foreach loop? Apr 27, 2024 am 11:33 AM

How to remove duplicate elements from PHP array using foreach loop?

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

PHP array key value flipping: Comparative performance analysis of different methods

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

PHP array multi-dimensional sorting practice: from simple to complex scenarios

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

Application of PHP array grouping function in data sorting

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

The role of PHP array grouping function in finding duplicate elements

See all articles