如何迭代使用双语对象的数组?
我有一个JSON响应的forms:
[{ "id": 425055, "title": "Foo" }, { "id": 425038, "title": "Bar" }, { "id": 425015, "title": "Narf" }]
我使用oboe.js创build高地stream:
const cruiseNidStream = _((push, next) => { oboe({ url: 'http://fake.com/bar/overview, method: 'GET', headers: { 'X-AUTH': 'some token', }, }).node('.*', (overview) => { // I expect here to get an object having and id, title property push(null, overview.id); }).done((overview) => { push(null, _.nil); }).fail((reason) => { console.error(reason); push(null, _.nil); }); });
我的问题是,我不知道什么模式使用节点,以便它匹配该数组的每个元素。 就目前来说,我用目前的设置得到的项目都是对象和属性:
425055 Foo { id: 227709, title: 'Foo' }
如果回应会有一个属性,如:
{ 'overview': [], }
我可以使用.overview.*
。
双簧pipe有两种匹配数据的方法,通过path和鸭子打字。
通过鸭子打字
oboe('/data.json') .node('{id title}', function(x) { console.log('from duck-typing', x) })
按path
oboe('/data.json') .node('!.*', function(x) { console.log('from path matching', x) }) //or also valid .node('!.[*]', function(x) { console.log('from path matching', x) })
在path示例中,注意!
字符。 这指的是树的根节点,这个模式将只有你的三个对象,而不是他们自己的任何属性进一步嵌套下来。
我做了一个gomix ,你可以在控制台中查看这个工作,并查看源代码 。
Oboe.js支持鸭子打字:
.node('{id title}', (overview) => { }
请注意,我的JSON是平的,所以这个工程。 嵌套json的结果可能会有所不同。