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;
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'm trying to check if string matches any of the strings saved in database, but with the code I have right now it checks only the first one
My code:
for (const key in keys) {
if (keys[key].key !== hashedQueryKey) {
return "Invalid Key provided.";
} else return true;
}
You should not return if the key does not match as you want to continue comparing keys. Something like:
function queryMatches(keys, hashedQueryKey) {
for (const key in keys) {
if (keys[key].key === hashedQueryKey) {
return true;
}
}
return false;
}
You could use Object.keys and Array.contains() to check if your key is present
if (Object.keys(keys).contains(hashedQueryKey)) {
return true;
} else {
return "Invalid Key provided.";
}
although looking at your code, and being paranoid ... just in case your 'key' property differs from the objects' key, using Object.keys and anyMatch() is safer ...
if (Object.keys(keys).anyMatch(key => keys[key].key === hashedQueryKey)) {
return true;
} else {
return "Invalid Key provided.";
}
if you don't need the message - just true or false , then you can just return the predicate.
ie.
return Object.keys(keys).contains(hashedQueryKey);
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";
}
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;
}
I'm experimenting with functional-style Javascript and have encountered an interesting situation. I have a foreach function that takes a collection and function object:
var Utils = {};
// Applies a functor to each item in a collection.
Utils.foreach = function(collection, functor)
{
for (var i = 0; i < collection.length; i++)
{
functor(collection[i]);
}
};
This is cool. However now I want to implement another function:
// Checks if a collection contains an item.
Utils.has = function(collection, item)
{
Utils.foreach(collection, function(obj) {
if (item === obj) {
return true; // How to force a return from the foreach function?
}
});
return false;
};
As you can see I can't implement the "has" function because my return statement doesn't break the iteration.
Can someone recommend a solution for this problem?
I guess what you want is not actually forEach, but rather some (other languages call it any). The couterpart is every (or all in other languages). You'll find an example implementation on MDC.
You need a modification to each.
Start by modifying has:
Utils.has = function (collection, item) {
var found = false;
Utils.foreach(collection, function (obj) {
if (item === obj) {
found = true;
return false;
}
});
return found;
};
Then you need to modify forEach to end early when it gets false
Utils.foreach = function (collection, functor) {
var prop;
for (prop in collection) {
if (prop.hasOwnProperty(prop)) {
if (functor(collection[prop]) === false) {
return;
}
}
}
};
Here is something real quick and untested ( it is friday 4:50 time to go home). I'll try to test and update this post later. see if this helps:
Utils = {};
Utils.foreach = function(collection, functor) {
loop: for (var i in collection) {
if (functor(collection[i])) {
alert("breaking the loop!");
break loop;
}
}
};
Utils.has = function(collection, item) {
var bolReturn = false;
Utils.foreach(collection, function(obj) {
if (item === obj) {
bolReturn = true;
return true;
}
return false;
});
return bolReturn;
};
Utils.has({"test":""}, "");
I don't think you need to abandon your structure-
why not throw and catch an error to break out of the loop?
Utils.has= function(collection, item){
try{
ControlCenter.foreach(collection, function(obj){
if(item=== obj){
throw 'found it!'
}
});
}
catch(er){
if(er== 'found it!') return true;
}
return false;
};