javascript - How to get last item in array Node.js? -


this question has answer here:

i new node.js , javascript question might quite simple cannot figure out.

i have lot of items in array want last item. tried use lodash somehow not provide me last item in array.

my array looks now:

images : ['jpg.item_1', 'jpg.item_2', 'jpg.item_3', ..., 'jpg.item_n'] 

and want get:

images : 'jpg.item_n' 

using lodash getting:

images : ['g.item_1', 'g.item_2', 'g.item_n'] 

it looks getting last letter in jpg, i.e. 'g'.

my code using lodash looks this:

const _ = require('lodash');    return getevents().then(rawevents => {      const eventstobeinserted = rawevents.map(event => {      return {          images: !!event.images ? event.images.map(image => _.last(image.url)) : []        }    })  })

your problem you're using _.last inside map. last character in current item. want last element of actual array.

you can pop(), should noted destructive (will remove last item array).

non-destructive vanilla solution:

var arr = ['thing1', 'thing2']; console.log(arr[arr.length-1]); // 'thing2' 

or, lodash:

_.last(event.images); 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -