I have a function defined like this:
var getPhoneNumber = function (list, phoneType) {
if (_.isEmpty(list)) {
return "Not Entered"
};
_.each(list, function(phoneNo){
if (phoneNo.name === phoneType) {
return phoneNo.value;
};
});
return "Not Entered";
}
list is an Array, while phoneType is a String. The problem is the function always returns the value Not Entered even if the list is not empty and has a phoneNo.name equal to phoneType. If I add a console.log in the if it shows that the condition is true and prints the console.log message but still returns Not Entered
return phoneNo.value; doesn't correspond to the function getPhoneNumber, but to the function passes as callback at _.each.
You should try something like this instead:
var getPhoneNumber = function (list, phoneType) {
var value = null;
_.each(list, function(phoneNo){
if (phoneNo.name === phoneType) {
value = phoneNo.value;
}
});
if(value !== null)
return value;
else
return "Not Entered";
}
Related
I have a function like this,
const isDisplayStaticAndConditionalSwitcher = (fieldNode, window) => {
const fieldDataSourceCode = fieldNode.getAttribute('DataSourceCode') || [];
const nodeValues = Object.values(window?.Designer?.nodes); // get the nodes values
const formDataSourceCode = nodeValues.map((o) => {
if (o.displayName === 'Form') { return o.props.code; }
}).filter((v) => v)[0];
return fieldDataSourceCode === formDataSourceCode;
};
I am getting the error, expected to return a value at the end of the arrow function error How should I resolve the issue?
Your lint rules want you to explicitly return undefined:
nodeValues.map((o) => {
if (o.displayName === "Form") {
return o.props.code;
} else {
return undefined;
}
});
The lint error is because of if condition inside map function. you need to return the same value or other in case of if condition fails.
Using map, Expectation from map function to return same length of Array.
const formDataSourceCode = nodeValues.map((o) => {
if (o.displayName === 'Form') { return o.props.code; }
// add the return in case if condition fails.
return o;
}).filter((v) => v)[0];
Hope this is helpful.
I made the code below:
mp.events.addCommand("admin", (player, targetPlayer, targetAdminLevel) => {
mp.players.forEach((targetPlayer2) => {
if (targetPlayer2 === targetPlayer) return true;
else return false;
});
try {
if (player.adminLevel < 8) throw "Error 1.";
if (!targetPlayer) throw "Error 2.";
if (player.adminLevel <= targetAdminLevel) throw "Error 3.";
//if (functionAbove = false) code
}
});
I want to know how to check if the function mp.players.forEach is true or false, so I can add it to the try statement. Thanks.
I'm trying to check if there's a player online with the name typed on the second argument of the command. Example: /admin playerName. If it exists, return true and set his adminLevel; if it doesn't, return false.
The function I'm using: https://wiki.rage.mp/index.php?title=Pool::forEach
Instead of forEach, use .some, .filter, .find. If you are returning something like given:
array.some(obj=><condition>) //return boolean
array.filter(obj=><condition>) //return array with all the elements which fulfills condition
array.find(obj=><condition>) //return element
You can iterate over object
const players = {
player1 : 'Neimar',
player2 : 'Carvalho',
player3 : 'Ronaldinho'
}
for (const [key, value] of Object.entries(players)) {
console.log(`${key}: ${value}`);
}
You can setup condition as you wish.
This is what I wanted to do:
global.findPlayer = function findPlayer(name) {
let players = mp.players.toArray();
for(let p in players) {
if(players[p].name == name) {
return players[p];
}
}
return null;
}
if (findPlayer(targetPlayer)) //code;
I have an variable that devolves an empty object, and i need validate thas this variable have a value.
guardar: function() {
var value1 = Ext.getCmp('radio1').getValue();
if (value1 === {}) {
alert('It is necessary to select an option.');
return;
}
}
When it arrives in the debug line in the If statement, although the value of the variable is {}, when evaluating the condition the result is false.
¿Someone could help me about how can i do that validation?
If you get a object, then, do you need to proccess that object to verify if exist a property
Try this:
guardar: function() {
var value1 = Ext.getCmp('radio1').getValue();
if (typeof value1 === 'object' && Object.keys(value1).length === 0) {
alert('It is necessary to select an option.');
return;
}
}
guardar: function() {
var value1 = Ext.getCmp('radio1').getValue();
if (Object.keys(value1).length === 0) {
alert('It is necessary to select an option.');
return;
}
} // This Will Work as your requirement
You can't do value1 === {} for the same reason that {} === {} is false. The reason for this is javascript compares objects by reference, not by value. Which means it checks if the objects occupy the same memory location.
You can try something like
Object.prototype.isEmpty = function() {
for(var key in this) {
if(this.hasOwnProperty(key))
return false;
}
return true;
}
Then you can test if it's empty
if (value1.isEmpty()) {
alert('It is necessary to select an option.');
return;
}
function isEmpty(obj) {
for(var key in obj) {
if(obj.hasOwnProperty(key)){
return false;
}
}
return true;
}
var x = {};
if(isEmpty(x)){
alert('It is necessary to select an option.');
return;
}else{
}
I have a method that takes a language abbreviation and matches it using a .constant dictionary, and returns the matching language name.
How can I do an evaluation with .filter to check whether the passed isoCode/language abbreviation exists?
Here is my method:
angular.module('portalDashboardApp')
.service('ISOtoLanguageService', ['Languages', function(Languages) {
this.returnLanguage = function(isoCode) {
var categoryObject = Languages.filter(function ( categoryObject ) {
return categoryObject.code === isoCode;
})[0];
return categoryObject.name;
};
}]);
Here is the method with some error catching I have tried:
angular.module('portalDashboardApp')
.service('ISOtoLanguageService', ['Languages', function(Languages) {
this.returnLanguage = function(isoCode) {
var categoryObject = Languages.filter(function (categoryObject) {
if (isoCode != null || isoCode != undefined) {
return categoryObject.code === isoCode;
}
else {
return categoryObject.code === 'und';
}
})[0];
if (categoryObject.name != undefined || categoryObject.name != null) {
return categoryObject.name;
}
else {
return "undefined";
}
};
}]);
Thank you!
I would recommend you organize your data at Languagesin an object or map, it'll be much faster and simpler when you fetch your translation by an abbreviation. A short example:
angular.module('portalDashboardApp')
.factory('Languages', function(){
var dictionary = {
ISO: {name: 'International Organization for Standardization'}
};
return {
get: function(abbr){
return dict[abbr];
}
};
}).service('ISOtoLanguageService', ['Languages', function(Languages) {
this.returnLanguage = function(isoCode) {
if(!isoCode) {
return "Answer for empty isoCode";
}
var categoryObject = Languages.get(isoCode);
return (categoryObject || {}).name || "I don't know this abbr";
};
}]);
I'm not sure that this JS works without any syntax error (I've not try to launch it) but idea is that you don't need array and filter on big dictionaries and you are able to get any abbreviation from dict with O(1) complexity even with huge dictionary.
If you don't want to have a refactoring with your code you can do something like this:
angular.module('portalDashboardApp')
.service('ISOtoLanguageService', ['Languages', function(Languages) {
this.returnLanguage = function(isoCode) {
if (!isoCode) {
return;
}
var resultAbbrs = Languages.filter(function (categoryObject) {
return categoryObject.code === isoCode;
});
if (resultAbbrs.length > 0) {
return resultAbbrs[0].name;
}
};
}]);
In this case if isoCode is null, undefined or empty string or this key is not found in dictionary return undefined will be by default. Outside you should check a result of this function with if (result === undefined) ...
I hope it helped you)
When calling my function checkIss(), issFullArray.indexOf(issToCheck) always returns undefined. I've run a .length, output the contents of issFullArray, I can't figure out why it's not working- the array looks fine to me. As you can see below, I've tried explicitly setting issArray as an array and copying the array returned by my getIssList()
function updateIss() {
var issArray = [];
var currService = current.u_business_service;
var currIss = current.u_is_service;
issArray = getIssList(currService).slice(); //getIssList() returns an arry
if (checkIss(issArray, currIss) === false) {
//do stuff
}
}
function checkIss(issFullArray, issToCheck) {
if (issFullArray.indexOf(issToCheck) < 0) {
return false;
} else {
return true;
}
}
Easiest to just loop through the array and compare each value and return true if there is a match otherwise return false. Not much more code and works for all browsers.
function checkIss(issFullArray, issToCheck) {
for(i=0; i<issFullArray.length; i++) {
if(issFullArray[i]==issToCheck) {
return true;
}
}
return false;
}