Want to shorten my if statement using Lodash - javascript

I am looking to optimize the below piece of code using Lodash. I have looked for solutions online, but could not find anything relevant. Can someone please help?
if (isPage && isPageType === constants.keys.isDisclosureSubmitted) {
if (form && form.application && typeof form.application.disclosureAccepted !== 'undefined' && form.application.disclosureAccepted !== null && !form.application.disclosureAccepted) {
return $q.when(response.data.content);
}
}

if (isPage && isPageType === constants.keys.isDisclosureSubmitted) {
const discAcc = _.get(form, ['application', 'disclosureAccepted'], true);
if (!_.isNil(discAcc) && !disAcc) {
return $q.when(response.data.content);
}
}
Basically, with _.get, you don't have to worry about checking a property of undefined, since it will just return undefined in that scenario rather than throw an error. _.isNil check is the value is both null or undefined, and then you want to make sure the value is still falsey.
I mean, I'm not sure if lodash "optimizes" in this case, since these function calls will actually slow it down, but it is slightly more readable. Like the comments, I'm not sure exactly why you need to be so specific; you could just use _.get(form, ['application', 'disclosureAccepted']) == false, thought I sway against using unstrict equivalence.

Related

Difference between if (array.length > 0) and if (array?.length) in Javascript (Why is array.length > 0 falsey?)

I'm a super green developer that started my first job last week. I got a comment from another developer to add a condition to check if array is undefined and to check if array is in fact an array.
Initially I used:
$: hasActiveCoupons = $cart.coupons && $cart.coupons.length > 0;
if (hasActiveCoupons && $cart.coupons.find((coupon) => coupon.id === couponCode)) {
...
}
But then she pointed out to me that $cart.coupons.length > 0 would return a falsey value. And corrected it by writing:
if ($cart.coupons?.length && $cart.coupons.find((coupon) => coupon.id === couponCode)) {
...
}
So I am wondering how this works? I tired reading a few other questions but I don't fully grasp this concept.
$cart.coupons.length > 0 will return a falsey value if coupons is empty, but there is no need to check length this way in this case. A smaller approach to this flow would be the following:
$cart.coupons?.find(({ id }) => id === couponCode);
If coupons is undefined, undefined is returned.
If no values satisfy the testing function inside find, undefined is returned.
Therefore, no need to test length separately.

If-statement not filtering out undefined

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&region=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);

Comparing problem with If else statement in Javascript

I have two condition of my same JSON data:
{
"selectionId":124,
"selectionDate":"2070-01-01",
"selectionAudit":null,
"letter":[
{
"letterNo":13,
"letterId":1,
"inout":"out",
"inoutNo":"12332466544",
"inoutDate":"2070-01-01",
"letterIssuedSubBy":null,
"letterFile":null,
"representativeName":null,
"representativeNameEng":null,
"selectionId":124,
"assessmentNo":null,
"entryBy":"user98",
"rStatus":"I",
"others":null,
"signatary":"qwerrt",
"letterBox":null,
"imageFile":null,
"imageTitle":null,
"reminderYesNo":"N"
}
]
}
Same JSON with letter array empty structure :
{
"selectionId":124,
"selectionDate":"2070-01-01",
"selectionAudit":null,
"letter":[]
}
All these values are stored in
var trDataSecondTable; . I tried to compare if the letter is empty or not by using if condition:
if(trDataSecondTable.letter != null) {
console.log("asasfasdfdsfdsfds");
$("#inoutDate").val(trDataSecondTable.letter[0].inoutDate);
$("#inoutNo").val(trDataSecondTable.letter[0].inoutNo);
$("#signatary").val(trDataSecondTable.letter[0].signatary);
}
else
{
console.log("entered in else part");
}
Though "letter":[] is empty it is not entering into else part. While comparing i also tried trDataSecondTable.letter.length != 0 but also it is not entering into else part.
Your condition should check for both null and the length:
if (trDataSecondTable.letter != null && trDataSecondTable.letter.length > 0)
Is is important to check for null before accessing the length property as it guarantees you won't try to access the length property on null, which would raise an exception. This is important since data is rarely reliable.
Because of the && operator, if the first check fails, the second check won't be evaluated.
Actually, an even safer way would be to check if the value is truthy, as this will work for null and undefined:
if (trDataSecondTable.letter && trDataSecondTable.letter.length > 0)
You could check the length of the array.
if (trDataSecondTable.letter.length) {
// letter has some elements
} else {
// no elements
}
I think this condition is enough
if(trDataSecondTable.letter.length)

Optimize a function that ensures an object's properties does not have any of these values

I have an array of objects that represent form fields. I want to make sure that none of the elements in the array have their initial values. As in all fields need to be updated. The initial value is below. I'd like a simple function that checks each element and ensures none of these properties have the default fields.
[
{
key: 0,
name: '',
typeLabel: '',
typeValue: 0,
value: '',
}
...
]
Looking for help on improving this
collection.every((obj) => {
if (obj.key === 0) return false;
if (obj.name === '') return false;
if (obj.typeLabel === '') return false;
if (obj.typeValue === 0) return false;
if (obj.value === '') return false;
return true;
});
EDIT: Actually just thought of this
collection.every((obj) => {
const values = Object.values(obj);
return values.includes(0, '');
});
To be honest I'm not sure how much more better you can make it, it looks pretty decent to me! Any changes I'd make would be subjective changes that don't actually matter. Curious what other people will answer with though, maybe I'm missing something :)
I mean you could do this and it's arguably less if/elses and uses a named function to explain what it is doing which is maybe nicer? But meh I don't think this matters all that much:
function inputValueHasBeenSet(inputObject) {
return !!inputObject.key &&
!!inputObject.name &&
!!inputObject.typeLabel &&
!!inputObject.typeValue &&
!!inputObject.value;
}
collection.every(inputValueHasBeenSet);

javascript checking multiple conditions in if statement

I have an if statement which checks multiple conditions. I would like it to skip the conditional statement if at least one thing returns false. I can see that it is currently picking up the conditional statement if at least one thing is true.
if((api != 'abosp') || (api !='sersp') || (api !='volsp') || (api !='consp') || (api !='givsp') || (api !='blosp')){
console.log("api: " + api );
api = 'cussp'
}
What would be the correct way to implement this kind of logic?
Currently if api == 'abosp' it will still pass into the conditional statement instead of skipping it.
You should change your || for and operator: &&. Still, a more "clean" method would be:
banned = ["abosp", "sersp", "volsp", "consp", "givsp", "blosp"]
if(banned.indexOf(api) == -1){ // if api is not in list
console.log("api: " + api );
api = 'cussp'
}
Actually your if can't be evaluated to true since api can't be all the values you check. I guess you need an and (&&) instead of an or (||):
if ((api != 'abosp') && (api !='sersp') && ...
In this case, you can use an associative array to have more succinct code:
d = {abosp: 1, sersp: 1, volsp: 1, consp: 1, givsp: 1, blosp: 1};
if (!d[api]) api = 'cussp';
I think you are looking for && rather than ||.
This will skip the conditional statement if any of the individual comparisions return false.

Categories