Home > Web Front-end > JS Tutorial > How to remove duplicate data when merging multiple arrays

How to remove duplicate data when merging multiple arrays

php中世界最好的语言
Release: 2018-04-28 15:17:21
Original
4699 people have browsed it

This time I will show you how to deduplicate data when merging multiple arrays. What are the precautions for deduplicating data when merging multiple arrays? The following is a practical case, let's take a look.

var arr1 = ['a','b'];
var arr2 = ['a','c','d'];
var arr3 = [1,'d',undefined,true,null];
//合并两个数组,去重
var concat_ = function(arr1,arr2){
  //不要直接使用var arr = arr1,这样arr只是arr1的一个引用,两者的修改会互相影响
  var arr = arr1.concat();
  //或者使用slice()复制,var arr = arr1.slice(0)
  for(var i=0;i<arr2.length;i++){
    arr.indexOf(arr2[i]) === -1 ? arr.push(arr2[i]) : 0;
  }
  return arr;
}
console.log(concat_(arr1,arr2));
Copy after login
Run result:

##

var arr1 = ['a','b'];
var arr2 = ['a','c','d'];
var arr3 = [1,'d',undefined,true,null];
//合并多个数组,去重
var concat = function(arr1,arr2,arr3){
  if(arguments.length <= 1){
    return false;
  }
  var concat_ = function(arr1,arr2){
    var arr = arr1.concat();
    for(var i=0;i<arr2.length;i++){
      arr.indexOf(arr2[i]) === -1 ? arr.push(arr2[i]) : 0;
    }
    return arr;
  }
  var result = concat_(arr1,arr2);
  for(var i=2;i<arguments.length;i++){
    result = concat_(result,arguments[i]);
  }
  return result;
}
console.log(concat(arr1,arr2,arr3));
Copy after login
Run result:

##
//合并多个数组,去重,排序
var arr1 = [1,6,4,0];
var arr2 = [8,20,7,4.5];
var arr3 = [6,0,7,90,2];
var concat = function(arr1,arr2,arr3){
  if(arguments.length <= 1){
    return false;
  }
  var concat_ = function(arr1,arr2){
    var arr = arr1.concat();
    for(var i=0;i<arr2.length;i++){
      arr.indexOf(arr2[i]) === -1 ? arr.push(arr2[i]) : 0;
    }
    return arr;
  }
  var result = concat_(arr1,arr2);
  for(var i=2;i<arguments.length;i++){
    result = concat_(result,arguments[i]);
  }
  //排序
  function sortNumber(a,b){
    return a - b;
  }
  return result.sort(sortNumber);
}
console.log(concat(arr1,arr2,arr3));
Copy after login

Run result:

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:

Detailed explanation of the use of React props and state attributes


jQuery implements mouse click suspension effects

The above is the detailed content of How to remove duplicate data when merging multiple arrays. 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