I am trying to cleanse a dataset, but my if statement appears not to work.
The first part, x.sites.length>0, seems to work by itself.
The second part, (x !== undefined), doesn't seem to register at all. In my return Array, some of the undefineds are still there. I have tried various different ways of writing (x !== undefined), but none of them seem to work for me.
Any thoughts?
export function clenseData(dataset){
let cleansedSet = dataset.map(x=>{
if(x.sites.length>0 && (x !== undefined))
{return x;}//doesn't work.
})
console.log(cleansedSet);
debugger;
return cleansedSet;
}
Here is the JSON String for one of the inputs: {"data":{"success":true,"data":[{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Roxanne Modafferi","Valentina Shevchenko"],"commence_time":1555754400,"home_team":"Roxanne Modafferi","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[3.35,1.3]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[3.8,1.27]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[3.35,1.3]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[3.75,1.28]}},{"site_key":"williamhill","site_nice":"William Hill","last_update":1555332878,"odds":{"h2h":[3.25,1.33]}}],"sites_count":5},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Marcin Tybura","Shamil Abdurakhimov"],"commence_time":1555754400,"home_team":"Marcin Tybura","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.57,2.3]}},{"site_key":"matchbook","site_nice":"Matchbook","last_update":1555333223,"odds":{"h2h":[1.63,2.39]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.55,2.4]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.57,2.3]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[1.6,2.31]}},{"site_key":"williamhill","site_nice":"William Hill","last_update":1555332878,"odds":{"h2h":[1.53,2.38]}}],"sites_count":6},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Aleksei Oleinik","Alistair Overeem"],"commence_time":1555754400,"home_team":"Alistair Overeem","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.9,1.4]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.9,1.4]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[2.96,1.41]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.9,1.41]}},{"site_key":"williamhill","site_nice":"William Hill","last_update":1555332878,"odds":{"h2h":[2.9,1.4]}}],"sites_count":5},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Keita Nakamura","Sultan Aliev"],"commence_time":1555754400,"home_team":"Sultan Aliev","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.8,1.95]}},{"site_key":"matchbook","site_nice":"Matchbook","last_update":1555333223,"odds":{"h2h":[1.85,1.97]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.81,2]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.8,1.95]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[1.86,1.92]}},{"site_key":"williamhill","site_nice":"William Hill","last_update":1555332878,"odds":{"h2h":[1.8,1.91]}}],"sites_count":6},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Alexander Volkov","Luis Henrique Da Silva"],"commence_time":1555786800,"home_team":"Luis Henrique Da Silva","sites":[{"site_key":"ladbrokes","site_nice":"Ladbrokes","last_update":1555333247,"odds":{"h2h":[1.74,2.05]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.66,2.23]}},{"site_key":"betfair","site_nice":"Betfair","last_update":1555332863,"odds":{"h2h":[1.66,2.18]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Marcin Tybura","Shamil Abdurakhimov"],"commence_time":1555790400,"home_team":"Shamil Abdurakhimov","sites":[{"site_key":"ladbrokes","site_nice":"Ladbrokes","last_update":1555333247,"odds":{"h2h":[1.55,2.4]}},{"site_key":"betfair","site_nice":"Betfair","last_update":1555332863,"odds":{"h2h":[1.53,2.48]}}],"sites_count":2},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Keita Nakamura","Sultan Aliev"],"commence_time":1555790400,"home_team":"Sultan Aliev","sites":[{"site_key":"ladbrokes","site_nice":"Ladbrokes","last_update":1555333247,"odds":{"h2h":[1.8,2]}},{"site_key":"betfair","site_nice":"Betfair","last_update":1555332863,"odds":{"h2h":[1.79,1.99]}}],"sites_count":2},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Roxanne Modafferi","Valentina Shevchenko"],"commence_time":1555790400,"home_team":"Roxanne Modafferi","sites":[{"site_key":"matchbook","site_nice":"Matchbook","last_update":1555333223,"odds":{"h2h":[3.78,1.3]}},{"site_key":"ladbrokes","site_nice":"Ladbrokes","last_update":1555333247,"odds":{"h2h":[3.7,1.27]}},{"site_key":"betfair","site_nice":"Betfair","last_update":1555332863,"odds":{"h2h":[3.45,1.28]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Aleksei Oleinik","Alistair Overeem"],"commence_time":1555801200,"home_team":"Aleksei Oleinik","sites":[{"site_key":"betfair","site_nice":"Betfair","last_update":1555332863,"odds":{"h2h":[3.05,1.45]}},{"site_key":"matchbook","site_nice":"Matchbook","last_update":1555333223,"odds":{"h2h":[2.95,1.42]}},{"site_key":"ladbrokes","site_nice":"Ladbrokes","last_update":1555333247,"odds":{"h2h":[3,1.39]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Alex Oliveira","Mike Perry"],"commence_time":1556406000,"home_team":"Mike Perry","sites":[],"sites_count":0},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Cory Sandhagen","John Lineker"],"commence_time":1556406000,"home_team":"Cory Sandhagen","sites":[{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.15,1.65]}},{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.15,1.65]}}],"sites_count":2},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Jack Hermansson","Ronaldo Souza"],"commence_time":1556413200,"home_team":"Jack Hermansson","sites":[{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.15,1.7]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.25,1.62]}},{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.25,1.62]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Glover Teixeira","Ion Cutelaba"],"commence_time":1556413200,"home_team":"Glover Teixeira","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.2,1.62]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.24,1.65]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[2.31,1.63]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.2,1.62]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Court McGee","Dhiego Lima"],"commence_time":1556413200,"home_team":"Court McGee","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.62,2.2]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.6,2.33]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.62,2.2]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Andrei Arlovski","Leonardo Augusto Guimaraes"],"commence_time":1556413200,"home_team":"Andrei Arlovski","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.1,1.68]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.22,1.66]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.1,1.68]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Jessica Penne","Jodie Esquibel"],"commence_time":1556413200,"home_team":"Jessica Penne","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.55,2.35]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.51,2.55]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.55,2.35]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Jason Gonzalez","Jim Miller"],"commence_time":1556413200,"home_team":"Jim Miller","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.25,1.6]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.27,1.63]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.25,1.6]}}],"sites_count":3},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Alex Volkanovski","Jose Aldo"],"commence_time":1557622800,"home_team":"Jose Aldo","sites":[{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.05,1.72]}},{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.05,1.72]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.09,1.74]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[2.12,1.74]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Jessica Andrade","Rose Namajunas"],"commence_time":1557626400,"home_team":"Rose Namajunas","sites":[{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.65,2.2]}},{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.65,2.2]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.71,2.13]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[1.7,2.18]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Alexander Gustafsson","Ashlee Evans Smith"],"commence_time":1559422800,"home_team":"Alexander Gustafsson","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.44,2.7]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.48,2.6]}}],"sites_count":2},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Jessica Eye","Valentina Shevchenko"],"commence_time":1560045600,"home_team":"Valentina Shevchenko","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[6,1.12]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[6,1.12]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[6.25,1.12]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[8,1.08]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Henry Cejudo","Marlon Moraes"],"commence_time":1560049200,"home_team":"Henry Cejudo","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[2.1,1.72]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[2.1,1.72]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[2.12,1.74]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[2.09,1.74]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Amanda Nunes","Holly Holm"],"commence_time":1562464800,"home_team":"Amanda Nunes","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.3,3.45]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.3,3.45]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.3,3.5]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[1.31,3.52]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Jon Jones","Thiago Santos"],"commence_time":1562468400,"home_team":"Jon Jones","sites":[{"site_key":"unibet","site_nice":"Unibet","last_update":1555332974,"odds":{"h2h":[1.14,5.5]}},{"site_key":"sport888","site_nice":"888sport","last_update":1555332928,"odds":{"h2h":[1.14,5.5]}},{"site_key":"nordicbet","site_nice":"Nordic Bet","last_update":1555332998,"odds":{"h2h":[1.17,5]}},{"site_key":"marathonbet","site_nice":"Marathon Bet","last_update":1555332887,"odds":{"h2h":[1.16,5.4]}}],"sites_count":4},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Colby Covington","Kamaru Usman"],"commence_time":1562475600,"home_team":"Colby Covington","sites":[],"sites_count":0},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Dustin Poirier","Khabib Nurmagomedov"],"commence_time":1568523600,"home_team":"Dustin Poirier","sites":[],"sites_count":0},{"sport_key":"mma_mixed_martial_arts","sport_nice":"MMA","teams":["Israel Adesanya","Robert Whittaker"],"commence_time":1569697200,"home_team":"Israel Adesanya","sites":[],"sites_count":0}]},"status":200,"statusText":"","headers":{"x-requests-remaining":"24","content-type":"application/json;charset=utf-8","x-requests-used":"126"},"config":{"transformRequest":{},"transformResponse":{},"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"headers":{"Accept":"application/json, text/plain, */*"},"method":"get","url":"https://api.the-odds-api.com/v3/odds/?apiKey=b80b98e306cbe877f35c7db3804d2d72&sport=mma_mixed_martial_arts®ion=uk&mkt=h2h"},"request":{}}
Are you trying to filter the data? if so you should be using filter function & not map. The map is used when you want to iterate over the data.
export function clenseData(dataset){
let cleansedSet = dataset.filter(x => {
if(x !== undefined && x.sites.length > 0) {
return true; // if you want to include it in the cleaned data
}
})
return cleansedSet;
}
The every() method tests whether all elements in the array pass the test implemented by the provided function:
dataset.every((x) => x !== undefined && x.sites.length> 0);