Lodash -- findIndex

1.引入

在script標簽中引入。

findIndex可以定義為 FindIndex、 .findIndex、 _.findIndex

import findIndex from 'lodash/findIndex'

2.返回參數中對象的索引號

設置一個dataList數組

var dataList =[
  {
    "user": "aa",
    "age": 11
  },
  {
    "user": "bb",
    "age": 22
  },
  {
    "user": "cc",
    "age": 33
  }
]

返回該數組的索引號.(寫數組任意一個對象都行)

findIndex(dataList,{"user": "aa"})   //=> 1
findIndex(dataList,{"age": 11})      //=> 0
findIndex(dataList,{"user": "cc","age": 33})   //=> 2

3.取值

取dataList中user為aa的年齡

dataList[findIndex(dataList,{'user':'aa'})].age

4.刪除

刪除數組中年齡為22的項

dataList.splice(findIndex(dataList,{"age":22}),1)

參考文檔:http://lodashjs.com/docs/#_findindexarray-predicate_identity-thisarg

推薦 JSON 格式化工具:http://www.jsoneditoronline.org/

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容