【问题标题】:primeng: How to find particular value in selectItem array using codeprimeng:如何使用代码在 selectItem 数组中查找特定值
【发布时间】:2016-09-30 05:51:24
【问题描述】:

请帮助我了解如何在primeng SelectItem[] 数组中找到特定值。

我有如下 selectItem 数组。

  this.cities = [];
  this.cities.push({label:'Select ', value:null});
  this.cities.push({label:'New York', value:{id:1, name: 'New York', code: 'NY'}});
  this.cities.push({label:'Rome', value:{id:2, name: 'Rome', code: 'RM'}});
  this.cities.push({label:'London', value:{id:3, name: 'London', code: 'LDN'}});
  this.cities.push({label:'Istanbul', value:{id:4, name: 'Istanbul', code: 'IST'}});
  this.cities.push({label:'Paris', value:{id:5, name: 'Paris', code: 'PRS'}});

我想从上面的列表中找到特定的项目({id:5,name:'Paris',code:'PRS'})。或者我只想找到代码为“PRS”的商品。

【问题讨论】:

    标签: primeng


    【解决方案1】:

    您可以使用以下任何一种方法。但最好的方法是“find”。

    查找值为 {id:5,name:'Paris',code:'PRS'}的项目

    let obj = {id:5, name: 'Paris', code: 'PRS'};
    var x = this.cities.find(item => 
        JSON.stringify(item.value) === JSON.stringify(obj)
    );
    
    console.log(x);
    

    let obj = {id:5, name: 'Paris', code: 'PRS'};
    this.cities.forEach((item,key) => {
        if(item && JSON.stringify(item.value) === JSON.stringify(obj)) {
            console.log("Item found at location : "+key)
        }
    });
    

    查找代码为“PRS”的项目

    this.cities.forEach((item,key) => {
        if(item && item.value != null && item.value.code == 'PRS') {
            console.log("Item found at location : "+key)
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 2012-09-04
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多