Home > Web Front-end > JS Tutorial > How to use JS to merge multiple arrays for recalculation

How to use JS to merge multiple arrays for recalculation

php中世界最好的语言
Release: 2018-05-25 13:54:38
Original
3142 people have browsed it

This time I will show you how to use JS to merge multiple arrays for recalculation, and how to use JS to merge multiple arrays for recalculation. What are the things to note?The following is a practical case, let’s take a look together take a look.

The example in this article describes the deduplication algorithm for merging multiple arrays implemented in JS. Share it with everyone for your reference, the details are as follows:

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

Running results:

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

Running results:

//合并多个数组,去重,排序
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 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:

Detailed explanation of the steps for using antd drop-down box linkage

How to develop a custom library

The above is the detailed content of How to use JS to merge multiple arrays for recalculation. 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