RE: RE: JavaScript开发笔记
You are viewing a single comment's thread from:

RE: JavaScript开发笔记

Words
0
Reading
0 min
9M
<input type="text" v-model="keywords" placeholder="搜索">
<tr v-for="list in search(keywords)"></tr>

//第一种方法 filter + includes
这是模糊查询
search(keywords){
  return this.lists.filter(item => {
    if(item.name.includes(keywords)){
      return item
    }
  })
}

steemposts.forEach(item => {
  if( JSON.parse(item.json_metadata).tags.includes('starnote')){
      res.push(item)
      console.log(111, 'okok!')
  }
})

let res = steemposts.filter(item => {
  if( JSON.parse(item.json_metadata).tags.includes('starnote') ){
      console.log(111, 'okok!')
      return item
  }
})


//检测数组 site 是否包含 runoob :
let site = ['runoob', 'google', 'taobao'];
 site.includes('runoob'); 
// true 
 site.includes('baidu'); 
// false

//第二种方法 indexOf
这是精确查询
indexOf()完整语法:
    array.indexOf(item,start) 
    参数: 
    item:必须。查找的元素。 
    start:可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的首字符开始检索。
if(arr.indexOf(某元素) > -1){//则包含该元素}

let fruits=["Banana","Orange","Apple","Mango","Banana","Orange","Apple"]
let a = fruits.indexOf("Apple")
console.log(123, a)  //2

search(keywords){
  let newlists=[]
  this.lists.forEach(item => {
    if(item.name.indexOf(keywords) != -1){
      newlists.push(item)
    }
  })
  return newlists
}

注:string.indexOf()返回某个指定的字符串值在字符串中首次出现的位置。
   查找字符串最后出现的位置,使用 lastIndexOf() 方法。
@lemooljiang: <input | Ecency