node.js - Getting object as array in javascript with array.map() function -


when using array.map function getting data in object. however, need in array. problem occurs function images.

ebit:

part of code involving array.map function looks following (sorry if code seems confusing given problem @ hand, there seemed confusion since had left out rest of code before):

function getvenues(page, size, venues) {    page = page || 0;    size = size || 200;    venues = venues || [];    return axios      .get(`https://app.ticketmaster.com/discovery/v2/venues.json?apikey=myapikey&page=${page}&size=${size}&countrycode=dk`)      .then(response => response.data._embedded.venues)      .then(rawvenues => {        rawvenues.foreach(venue => venues.push(venue));        if (rawvenues.length < size) {          // done return compiled list.          return venues;        }        // recurse on next set of venues adding promise chain.        return getvenues(page + 1, size, venues);      });  }    function getevents(page, size, events) {    page = page || 0;    size = size || 200;    events = events || [];    return axios      .get(`https://app.ticketmaster.com/discovery/v2/events.json?apikey=myapikey&countrycode=dk&size=${size}&page=${page}`)      .then(response => response.data._embedded.events)      .then(rawevents => {        rawevents.foreach(event => events.push(event));        if (rawevents.length < size) {          // done return compiled list.          return events;        }        // recurse on next set of events adding promise chain.        return getevents(page + 1, size, events);      });    }    app.get('/tm2', (req, res) => {    getvenues().then(rawvenues => {        const venuestobeinserted = rawvenues.map(venue => {        return {          sourceid: venue.id,          venue: venue.name,          postalcode: venue.postalcode,          city: venue.city.name,          country: venue.country.name,          countrycode: venue.country.countrycode,          address: !!venue.address ? venue.address.line1 : null,          longitude: !!venue.location ? venue.location.longitude : null,          latitude: !!venue.location ? venue.location.latitude : null,          source: 'ticketmaster'        };      });        // return promise errors bubble chain...      return venue.create(venuestobeinserted).then(venues => {        console.log("venues inserted");              // return promise errors bubble chain...        return getevents().then(rawevents => {            const eventstobeinserted = rawevents.map(event => {            return {              name: event.name.trim(),              slug: slugify(event.name.trim()).tolowercase(),              sourceid: !!event._embedded.venues ? event._embedded.venues.map(sourceids => {                return {                  sourceid: !!sourceids ? sourceids.id : []                }              }) : [],              tags: !!event.classifications ? event.classifications.map(tag => {                return {                  tags: !!tag ? [tag.segment.name, tag.genre.name] : [],                }              }) : [],              images: !!event.images ? event.images.map(image => {                return {                  images: !!image ? image.url : []                }              }) : []              }          })            // return promise errors bubble chain...          return event.create(eventstobeinserted).then(events => {            console.log("events inserted");            });        });      });    }).then(() => { // fired after of promises above have resolved...      return event.find({}).select({        genres: 1,        tag: 1      }).limit(30).populate('event').populate('venue').then(events => {        console.log(util.inspect(events));        res.send(events);      });    }).catch(err => { // catches error during execution.      console.error(err);      res.status(500).send(err);    });  });

my current output looks this:

images:     [ { images: 'url_1' },      { images: 'url_2' },      { images: 'url_3' } ]  

i want in following format:

images:     [ { 'url_1' },      { 'url_2' },      { 'url_3' } ]  

i avoid commenting further on how better organize code. now, obtain desired result, remove object literal you've wrapped each of calls map() with. example fixing images, change this:

images: !!event.images ? event.images.map(image => {   return {     images: !!image ? image.url : []   } }) : [] 

to this:

images: !!event.images ? event.images.map(image => {   return !!image ? image.url : '' }).filter(boolean) : [] 

i'm not sure why input event.images array contain malformed objects without url property, or missing itself, .filter(boolean) deals removing empty strings, specified in third expression of ternary operator ... ? ... : ''.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -