Home > Mobile >  Is there a way to get Random Top 10% result from an Angular Material Data Source
Is there a way to get Random Top 10% result from an Angular Material Data Source

Time:04-11

Apologies for the crudeness of the code. It's a work in progress. Whilst I feel that this is close to working I can't seem to push it over the finish line. I just simply want to push 10% of the results from http post request to the material table. Any advice appreciated.

RandomSample() {

    const headers = { 'content-type': 'application/json' };

    var userId, roleId, eventTypeIds: any[];
    if (this.selectedUser == undefined) {
      userId = 0;
    }
    else {
      userId = this.selectedUser.id;
    }
    if (this.selectedEventTypes == undefined) {
      eventTypeIds = [-1];
    }
    else {
      eventTypeIds = this.selectedEventTypes.map(selectedEventTypes => selectedEventTypes.id);
    }

    var SearchStart, SearchEnd
    if (this.searchStartDate == undefined) {

      var ssd = new Date();
      ssd.setDate(ssd.getDate() - 14);
      SearchStart = ssd;
    }
    else {
      SearchStart = this.searchStartDate;
    }

    if (this.searchEndDate == undefined) {

      SearchEnd = new Date();
    }

    else {
      SearchEnd = this.searchEndDate;
    }

    var body = JSON.stringify({ UserId: userId, EventTypeIds: eventTypeIds, SearchStartDate: SearchStart, SearchEndDate: SearchEnd });
    console.log(body);
    this.http.post(environment.BASE_URL   'useractivity/getloginsoverfourteendays', body, { 'headers': headers }).subscribe((selectedUser: any) => {
      var random = Math.floor(Math.random() * selectedUser.length);
      this.dataSource = new MatTableDataSource(selectedUser[random]);
      console.log();
    }, (error: any) => {
      console.error(error);
    })

  }

CodePudding user response:

We could use slice(startIndex, endIndex) to select range of items from this.selectedUser[] based on this.selectedUser.length itself. This will select 20% of the first items.

 selectedUser = Array.from(Array(100).keys()) // [1,2,3,...100]

 function httpCall() {
    const percent = 20;
    const strartIndex = 0;
    const endIndex = (percent * this.selectedUser.length / 100)
    const dataSource = this.selectedUser.slice(strartIndex,Math.floor(endIndex))
    console.log(dataSource)
  }
  
  this.httpCall()

To select same amount of items based on % given, but randomly we create a helper function which randomly selects items and returns array of chosen items (avoides dupes).

 selectedUser = Array.from(Array(100).keys()) // [1,2,3,...100]

 function httpCall() {
    const percent = 20;
    const dataSource = this.selectRandomItems(percent, this.selectedUser);
    console.log(dataSource);
  }

 function selectRandomItems(percent, selectedUser) {
    const totalCount = Math.floor((percent * this.selectedUser.length) / 100);
    let returnArr = [];
    while (returnArr.length < totalCount) {
      let item = selectedUser[Math.floor(Math.random() * selectedUser.length)];
      returnArr.push(item);
      returnArr = [...new Set(returnArr)];
    }
    return returnArr;
  }
  
  this.httpCall()

  • Related