4 Replies - 137 Views - Last Post: 30 October 2017 - 11:19 AM

#1 ggwp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-October 17

Merge two arrays outside their functions

Posted 30 October 2017 - 09:44 AM

Hi! I'm trying to merge two arrays by their id (event.id) into one array and then put it in Mongodb. I faced with a problem, I don't understand how to merge these arrays outside their functions. Thank you for your time. :)

var pinnacleAPI = require('./pinnacleAPI');
var pinnacle = new pinnacleAPI('username', 'password');


function getpinnacle() {

  return new Promise(function (resolve, reject) {

    var options = {sportId: 29};
    pinnacle.getFixtures(options, function(err, response, body) {
      if (err) throw new Error(err);
      var pinnFixtures = [];
      body.league.forEach(function(leagues){
        leagues.events.forEach(function(event){ 
          if (event.status == 'O'){
            pinnFixtures.push({
              'id': event.id,
              'homeTeamName': event.home,
              'awayTeamName': event.away
            });
          };    
        });
      });
    //  resolve(pinnFixtures);
    });

    var options = {sportId: 29, oddsFormat: "DECIMAL"};
    pinnacle.getOdds(options, function(err, response, body) {
      if (err) throw new Error(err);
      var pinnOdds = [];
      body.leagues.forEach(function(league){
        league.events.forEach(function(event){
          event.periods.forEach(function(period){
            if (period.moneyline !== undefined) {
              pinnOdds.push({
                'id': event.id,
                'homeTeamOdds': period.moneyline.home,
                'drawOdds': period.moneyline.draw,
                'awayTeamOdds': period.moneyline.away
              }); 
            };
          });
        });
      });
    //  resolve(pinnOdds);
    });
  });

  var mergedList = pinnFixtures.map(function(item){
    return item.extend(item, pinnOdds.findWhere({ id: item.id }));
});
  resolve(mergedList);
}

module.exports = getpinnacle;



Is This A Good Question/Topic? 0
  • +

Replies To: Merge two arrays outside their functions

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Merge two arrays outside their functions

Posted 30 October 2017 - 09:57 AM

You can't, because you made those arrays local to the callback functions, so once that function ends the array gets destroyed. You need to store it somewhere else, like in sessionStorage, so you can access it later. Each of those callback functions should call a third function which can merge the data and do whatever else you want with it, and that third function can also check another variable in sessionStorage to see if both arrays have been returned yet.
Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,051
  • Joined: 08-June 10

Re: Merge two arrays outside their functions

Posted 30 October 2017 - 10:17 AM

one way for that would be using Promise.all(), so you have both arrays in the same callback.
Was This Post Helpful? 1
  • +
  • -

#4 ggwp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-October 17

Re: Merge two arrays outside their functions

Posted 30 October 2017 - 11:17 AM

Could you please show me how to it? :)/>

This post has been edited by Dormilich: 30 October 2017 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,051
  • Joined: 08-June 10

Re: Merge two arrays outside their functions

Posted 30 October 2017 - 11:19 AM

see https://developer.mo...cts/Promise/all
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1