2016-11-25 26 views
0

我有一个用户数据对象(来自一个终极版店)包含此:我如何通过id找到我的对象lodash?

"packs": [ 
      { 
      "name": "Docos", 
      "id": "101", 
      "price": 1000, 
      "gifted": false 
      }, 
      { 
      "name": "Entertainment Plus", 
      "id": "102", 
      "price": 1000, 
      "gifted": false 
      }] 

我试图找到第一个1是这样的:

let pack = _.find(this.props.userData.packs, 'id', "101") 

但是当我运行它,它说包是不确定的?为什么?

+0

你的代码似乎是好的,https://jsfiddle.net/W4QfJ/3398/ – QoP

+0

@QoP试图通过 “102” 找到= ) – stasovlas

回答

1

读lodash文档和观看例子

_.find

参数

集合(阵列|对象):要检查的集合。

[predicate = _。identity](Function):每次迭代调用的函数。

[fromIndex = 0](number):要从中进行搜索的索引。

1速记

_.find(this.props.userData.packs, {id: '101'}) 

2速记

_.find(this.props.userData.packs, ['id', '101']) 
0

根据Lodash documentation,正确的语法将是以下之一:

_.find(this.props.userData.packs, function(p) { return p.id === '101'; }); 

_.matches迭代速记。

_.find(this.props.userData.packs, { 'id': '101' }); 

_.matchesProperty iteratee简写。

_.find(this.props.userData.packs, [ 'id': '101' ]); 

更多从lodash文档:https://lodash.com/docs/4.16.6#find

相关问题