美文网首页
Lodash -- findIndex

Lodash -- findIndex

作者: 彤_姑娘 | 来源:发表于2017-08-21 16:16 被阅读49次

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/

相关文章

网友评论

      本文标题:Lodash -- findIndex

      本文链接:https://www.haomeiwen.com/subject/uvxhdxtx.html