lodash用_.get()深深偷看

时间:2017-01-13 16:39:28

标签: javascript lodash

我有这样的数据结构

var data = {
  'app.state1': {
    some: {...},
    sequences: [
      {...},
      {...},
      {...}
    ]
  },
  'app.state2': {
    some: {...},
    sequences: [
      {...},
      {...}
    ]
  }
};

我希望在sequences

的索引0获取data['app.state1']

我试图像这样实现它

var seq = _.get(data, 
   'data[' + currentState + '].sequences[' + currentSequence +']');

或者像这样

var seq = _.get(data, 
   'data[\'' + currentState + '\'].sequences[' + currentSequence +']');

两者都不起作用......我相信currentState的问题是内部的点(app.state1app.state1.substate2例如)。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

你结合得多,检查一下:

var seq = _.get(data, [currentState, 'sequences', currentSequence]);