This is the 15th day of my participation in the August More Text Challenge.

An array of overlap

    var arr1 = [{name:'name1'.id:1}, {name:'name2'.id:2}, {name:'name3'.id:3}];
      var arr1Id = [1.2.3]
      var arr2 = [{name:'name1'.id:1}, {name:'name2'.id:2}, {name:'name3'.id:3}, {name:'name4'.id:4}, {name:'name5'.id:5}];
      var result = arr2.filter(function(v){
            returnarr1Id.indexOf(v.id)! = = -1 // Use the filter method to see if there are the same elements
        })
      console.log(result);
Copy the code

Array and set

 let arr1 = [{name:'name1'.id:1}, {name:'name2'.id:2}, {name:'name3'.id:3}];
      let arr2 = [{name:'name1'.id:1}, {name:'name4'.id:4}, {name:'name5'.id:5}];
      let arr3 = arr1.concat(arr2);
      let result = [];
      var obj = [];
      result = arr3.reduce(function(prev, cur, index, arr) {
        console.log(prev, cur);
        obj[cur.id] ? ' ' : obj[cur.id] = true && prev.push(cur);
        returnprev; } []);console.log(result);
Copy the code

An array of difference set

let arr1 = [{name:'name1'.id:1}, {name:'name2'.id:2}, {name:'name3'.id:3}];

let arr1Id = [1.2.3];

let arr2 = [{name:'name1'.id:1}, {name:'name4'.id:4}, {name:'name5'.id:5}];

let arr2Id = [1.4.5];

let arr3 = arr1.concat(arr2);

let result = arr3.filter(function(v){

return arr1Id.indexOf(v.id)===-1 || (arr2Id.indexOf(v.id)===-1)})` console.log(result);Copy the code

Array to heavy

4. To heavy

let arr = [{name:'name1'.id:1}, {name:'name2'.id:2}, {name:'name3'.id:3}, {name:'name1'.id:1}, {name:'name4'.id:4}, {name:'name5'.id:5}];
      var obj = [];
      let result = arr.reduce(function(prev, cur, index, arr) {
        console.log(prev, cur);
        obj[cur.id] ? ' ' : obj[cur.id] = true && prev.push(cur);
        returnprev; } []);Copy the code

Array deduplicate other methods

Methods a

let includeThis = false
            let vm = this
            if(vm.informedPersonList.length>0){
                vm.informedPersonList.forEach(el= >{
                    if(el.id == vm.selectedTrueItem.id){
                        includeThis = true}})}if(includeThis===false){
                vm.informedPersonList.push(vm.selectedTrueItem)
            }else{
                Message({message: 'Do not repeat'.type: 'warning'})}Copy the code

Method 2Use the Set method in ES6 to remove the weight

let arr = [1.0.0.2.9.8.3.1];
          function unique(arr) {
                return Array.from(new Set(arr))
          }
          console.log(unique(arr));   / / or6console,0,2,9,8,3 [1]. The log (... New to the Set (arr)); / /,0,2,9,8,3 [1]
Copy the code

Use a double for loop, then use the array splice method to duplicate.

var arr = [1.5.6.0.7.3.0.5.9.5.5];
function unique(arr) {
    for (var i = 0, len = arr.length; i < len; i++) {
        for (var j = i + 1, len = arr.length; j < len; j++) {
            if (arr[i] === arr[j]) {
                arr.splice(j, 1);
                j--;        // The value of j decreases by 1 for every number removed
                len--;      // Len should be reduced by 1 when j is reduced.
                // console.log(j,len)}}}return arr;
}
console.log(unique(arr));       // 1, 5, 6, 0, 7, 3, 9
Copy the code