我是新来的node.js和JavaScript,所以这个问题可能很简单,但我不明白。如何获取数组Node.js中的最后一项?
我有一个数组中的很多项目,但只想获得最后一个项目。我试图使用lodash,但它不知道如何提供数组中的最后一项。
我的阵列看起来像现在这样:
images : ['jpg.item_1', 'jpg.item_2', 'jpg.item_3', ..., 'jpg.item_n']
,我想:
images : 'jpg.item_n'
使用lodash我越来越:
images : ['g.item_1', 'g.item_2', 'g.item_n']
它看起来像只是我以jpg获得最后一个字母,即'g'。
使用lodash我的代码如下所示:
const _ = require('lodash');
return getEvents().then(rawEvents => {
const eventsToBeInserted = rawEvents.map(event => {
return {
images: !!event.images ? event.images.map(image => _.last(image.url)) : []
}
})
})
我明白了,这很有道理。但是当我想要在eventsToBeInserted中得到结果时,我该如何去解决它? –