簡體   English   中英

Vue JS搜索數組

[英]Vue js searching array

我正在嘗試在數組中搜索json對象上的查詢字符串。 返回的字段是過濾結果的數組。 當前,列表返回的是名稱字段,而不是數字字段。

      computed: {
        search: function () {
          let self = this
          let filtered = []

          filtered = self.jcontacts.filter(function(contact){
            return  contact.firstname.toLowerCase().indexOf(self.query.toLowerCase())>=0 ||
                    contact.lastname.toLowerCase().indexOf(self.query.toLowerCase())>=0;
                    contact.email.toLowerCase().indexOf(self.query.toLowerCase()) >=0 ||
                    contact.phonenumber.toLowerCase().indexOf(self.query.toLowerCase()) >=0;
            }
          );
          return this.contacts = filtered
        }
      },
    }

搜索方法中的過濾方法未顯示該數字。 json的示例如下:

[{“ id”:1,“ phoneNumber”:[“ 3908902”],“ email”:[“ jamie@fox.com”],“ firstname”:“ Jamie”,“ lastname”:“ Fox”}]

  1. 當心case phoneNumber != phonenumber
  2. phoneNumber存儲為數組而不是字符串,因此您不能像這樣查找它(例如,使用.includes()函數)
  3. 對於代碼格式化,請考慮將self.query.toLowerCase()存儲為變量或其他計算的屬性

這是一個錯字 簽出字段phoneNumber被過濾為電話號碼。

它是一個數組,因此您可以按以下方式進行操作:

contact.phoneNumber.forEach( number => { 
   if(number.toLowerCase().indexOf(self.query.toLowerCase()) >=0) 
      return true;
});

我認為類似地,您也可以將其記入電子郵件,因為它也是一個數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM