This is the second day of my participation in Gwen Challenge

For the convenience of testing the code, we have written the function 😀 that can be printed on the console

Find objects with different/identical ids in both arrays

let arr1 = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }]
let arr2 = [{ id: 1 }, { id: 2 }, { id: 3 }]
let arr3 = arr1.filter(item= > {
	let idList= arr2.map(v= > v.id)
  return! idList.includes(item.id) })console.log(arr3)
Copy the code

Merge array of the same object, add property values

var a = [
  {id:1.num:"12"},
  {id:2.num:"13"},
  {id:3.num:"3"},
  {id:2.num:"16"},
  {id:5.num:"14"},
  {id:1.num:"14"}]var b = []
for(var i = 0; i < a.length; i++){for(var j = a.length-1; j>i; j--){if(a[i].id == a[j].id){
            a[i].num = (a[i].num*1 + a[j].num*1).toString()
            b.push(j)
        }
    }

}
for(var k = 0; k<b.length; k++){ a.splice(b[k],1)}console.log(a)
Copy the code

Count the number of repeated occurrences of an element in an array

let list = ["Li"."Li"."Set"."Brother"."Brother"."Raw"."Raw"."Li"];
let counts = list.reduce((a, v) = > v === "Li" ? a + 1 : a + 0.0);
console.log(counts);/ / 3
Copy the code

Count the number of repeats of each element in the array

var arr = ["Li"."Li"."Set"."Brother"."Brother"."Raw"."Raw"."Li"]; 

function getRepeatNum1(){ 
    var obj = {}; 
    for(var i= 0, l = arr.length; i< l; i++){ 
        var item = arr[i]; 
        obj[item] = (obj[item] +1) | |1; 
    } 
    return obj; 
}
function getRepeatNum2(){ 
    return arr.reduce(function(prev,next){ 
        prev[next] = (prev[next] + 1) | |1; 
        returnprev; }, {}); }console.log(getRepeatNum1());//{li: 3, set: 1, di: 2, sheng: 2}
console.log(getRepeatNum2());//{li: 3, set: 1, di: 2, sheng: 2}
Copy the code

Array to find the different elements

let sameArr = (arrOne,arrTwo) = > arrOne.filter(item= > arrTwo.findIndex(subItem= > subItem.id == item.id) === -1)
sameArr([{id:12}, {id:13}], [{id:20}, {id:13}])//[{id:12}]
Copy the code

Sort by time in an array

let arr = [{Date:'2020-10-21'}, {Date:'2020-10-20'}, {Date:'2020-10-22'}]
arr.sort((a, b) = > new Date(b.Date.replace(/-/g.'/')).getTime() - new Date(a.Date.replace(/-/g.'/')).getTime())
console.log(arr)
Copy the code

Filter properties in an array object

let arr =[
{
  "id": "b3bae8fcde0d4a50b7fbcbbccf4fe216"."sex": 2."userAccount": "chenghua"."userName": "lomo"}];let afterArr = arr.map(item= >{
	let obj ={
      "sex": ' '."userAccount": ""."userName": ""
	}
  Object.keys(obj).forEach(key= >{obj[key]=item[key]})
	return obj
})
/ / or
let afterArr = arr.map(item= >{
	return {
      sex: item.sex,
      userAccount: item.userAccount,
      userName: item.userName
	}
})
console.log(afterArr)
Copy the code

Two array objects that have the same property value complete the other array object property value

let a = [
    {
        'name': 'wang'.'age': '18'.'six': '1'}];let b = [
    {
        'name': 'wang'.'age': '8'.'six': ' '}];for (let i = a.length; i--;) {
    for (let j = b.length; j--;) {
        if (a[i].name === b[j].name) {
            b[j].six = a[i].six;
            break; // Exit the loop if the condition is met}}}console.log(b);//[{name: 'wang', age: '8', six: '1'}]
Copy the code

The tree array recursively retrieves the subitem data

/ / data
let data1=  [
    {
        title: 'parent 1'.expand: true.children: [{title: 'parent 1-1'.expand: true.tag:true.children: [{title: 'leaf 1-1-1'
                    },
                    {
                        title: 'leaf 1-1-2'}]}, {title: 'parent 1-2'.expand: true.tag:true.children: [{title: 'leaf 1-2-1'
                    },
                    {
                        title: 'leaf 1-2-1'}]}]function getInnerMenuList(menuTreeList, menuList) {
      for (let item of menuTreeList) {
        /* If you want to get data from a certain level, you need to get data from a certain level. If you want to get data from a certain level, you need to get data from a certain level. Item. tag can be a recursive conditionitem.children? getInnerMenuList(item.children, menuList):menuList.push(item) } };let arr = [];
    getInnerMenuList(data1,arr);
    console.log(arr);
    //0: {title: "leaf 1-1-1"}
    //1: {title: "leaf 1-1-2"}
    //2: {title: "leaf 1-2-1"}
    //3: {title: "leaf 1-2-1"}
Copy the code

Filter another array based on a conditional array

// Conditional array
let ageList = [12.16]; 
// The filtered array
let objList = [
        {name:'tom'.age:12},
        {name:'jack'.age:33},
        {name:'zio'.age:12},
        {name:'lolo'.age:89},
        {name:'robin'.age:16},]/ / 1
let data = objList.filter((item/) = > ageList.includes(item.age));
console.log(data)

/ / 2
let map = {};
let ret = [];
objList.forEach(item= > (map[item.age] = map[item.age]||[]).push(item));
ageList.forEach(item= > map[item] && [].push.apply(ret, map[item]));
console.log(ret);
Copy the code

Sum of properties in an array object

var result = [
    {
        subject: 'math'.score: 10
    },
    {
        subject: 'chinese'.score: 20
    },
    {
        subject: 'english'.score: 30}];let sum = result.reduce((prev, cur) = > cur.score + prev, 0);
console.log(sum) / / 60
Copy the code

Version number comparison

//1 "2.2.3", "v2.2.3" applicable or not applicable to "2.3.0a"
function compareVersion(a,b){
    function toNum(a){
      var a=a.toString();
      var c=a.split(/\D/);
      var num_place=[""."0"."00"."000"."0000"],r=num_place.reverse();
      for (var i=0; i<c.length; i++){var len=c[i].length;
                 c[i]=r[len]+c[i];
       }
      var res= c.join(' ');
      return res;
     } 
   var _a=toNum(a),_b=toNum(b);
   if(_a==_b) return 'equal'
   if(_a>_b)  return 'more than'
   if(_a<_b)  return 'less than'
}
compareVersion("Then"."2.3.0");
compareVersion("v2.2.3"."v2.3.0");

//2 "2.2.3", "v2.2.3", "2.3.0a" all apply
function cpr_version(a,b){
    Array.prototype.triming=function(){
       var  arr=[];
       this.forEach(function(e){
           if(e.match(/\S+/))  arr.push(e);
       })
       return arr;
    }
    // Extract the numeric part
    function toNum(a){
        var a=a.toString();
        var c=a.split(/\D/).triming();
        var num_place=[""."0"."00"."000"."0000"],r=num_place.reverse();
        for (var i=0; i<c.length; i++){var len=c[i].length;
            c[i]=r[len]+c[i];
        }
        var res= c.join(' ');
        return res;
    } 
    // Extract characters
    function toChar(a){
      var a=a.toString();
      var c=a.split(/\.|\d/).join(' ');
      return c;
    }
    let _a1=toNum(a),_a2= toChar(a), _b1=toNum(b),_b2= toChar(b);
    if(_a1>_b1)  console.log("Version Number"+a+"It's a new version!);  
    if(_a1<_b1)  console.log("Version Number"+b+"It's a new version!); 
    if(_a1===_b1)  {
        _a2=_a2.charCodeAt(0).toString(16);
        _b2=_b2.charCodeAt(0).toString(16);
        if (_a2>_b2)  console.log("Version Number"+a+"It's a new version!);   
        if(_a2<_b2)   console.log("Version Number"+b+"It's a new version!); 
        if(_a2===_b2) console.log("Same version number! Version number:"+a);
     }
    }
    cpr_version("2.3.0 a."."2.3.0 b");
    cpr_version("2.3.0 c"."2.3.0 c");
Copy the code