如何找到数组angular 6中最接近的下限值?

时间:2019-05-26 09:27:45

标签: html angular typescript

我有一个对象数组,我需要根据角度6中的某个值或当前值从该数组中找到最接近的下限值,我已经尝试过了,但是其显示不确定。

app.component.html

<button (click) ="getPrev()">Previous Value</button>

app.component.ts

declare var require: any;
import { Component } from '@angular/core';
import { OnInit } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent{
    arrayVal:any;
    currentVal : any;
  title = 'projectchart';
  public array = [{"id":1},{"id":3},{"id":5}];


  getPrev(){
     this.currentVal = 5;
     this.arrayVal= this.array; 
     let number = this.arrayVal.reverse().find(e => e <= this.currentVal);
     console.log(number);
  }
}

5 个答案:

答案 0 :(得分:0)

您的数组中有ID,因此-

 let number = this.arrayVal.reverse().find(e => e.id <= this.currentVal);

不会是不确定的

答案 1 :(得分:0)

将功能更改为:

 getPrev(){
 this.currentVal = 5;
 this.arrayVal= this.array; 
 let number = this.arrayVal.reverse().find(e => e.id <= this.currentVal);
 console.log(number);

}

答案 2 :(得分:0)

您可以找到以下最近的壁橱编号

let arr = [
    { "id" : 1 },
    { "id" : 3 },
    { "id" : 5 }
];

let referenceNumber = 4;

let nearestLowest = arr.reduce((acc, val) => {
    if(val['id'] < referenceNumber && val['id'] > acc)
        acc = val['id'];
    return acc;
}, Math.max());

console.log(nearestLowest);

答案 3 :(得分:0)

尝试这个。

  getPrev(){
     this.currentVal = 5;
     this.arrayVal= this.array; 
     let number = this.arrayVal.reduce((prev, current) => {
       return (Math.abs(current.id - this.currentVal) < Math.abs(prev.id - this.currentVal) ? current : prev);
     })
     console.log(number.id);
  }

Stackblitz

答案 4 :(得分:0)

如果您的ID可以为负数,则可以使用以下代码:

{% if is_paginated %}
<nav>
  <ul class="pagination">
    <li class="page-item">
      <a href="{% url 'core:MovieList'%}?page=1" class="page-  link">First</a>
    </li>
    {% if page_obj.has_previous %}
      <li class="page-item">
        <a href="{% url 'core:MovieList' %}?page={{page_obj.previous_page_number}}" class="page-link">{{page_obj.previous_page_number}}</a>
      </li>
    {% endif %}
    <li class="page-item active">
      <a href="{% url 'core:MovieList' %}?page={{page_obj.number}}" class="page-link">{{page_obj.number}}</a>
    </li>
    {% if page_obj.has_next %}
      <li class="page-item">
        <a href="{% url 'core:MovieList' %}?page={{page_obj.next_page_number}}" class="page-link">{{page_obj.next_page_number}}</a>
      </li>
    {% endif %}
    <li class="page-item">
      <a href="{% url 'core:MovieList' %}?page={{paginator.num_pages}}" class="page-link">Last</a>
    </li>
  </ul>
</nav>
{% endif %}

#View
class MovieListView(ListView):
    model = Movie
    template_name = 'movie_list.html'

您可以像这样使用它:

getClosestLowerIdElement(value: number, array: Array<{[key: string]: number}>) {
    const v = {id: value};
    return array.reduce((acc, a) => acc = (
        (acc.id === undefined || Math.abs(v.id - a.id) < Math.abs(v.id - acc.id) || 
            acc.id > v.id) && a.id < v.id) ? a : acc, {id: undefined});
  }

如果没有比传递给idArray = [{id: 1}, {id: -30}, {id: -5}, {id: 7}, {id: 9}, {id: 11}, {id: 21}]; ... console.log(this.getClosestLowerIdElement(0, this.idArray)); // prints {id: -5} 方法的值小的数字,您将得到getClosestLowerIdElement

查看Stackblitz上的演示