How to check if string value is in array - arrays

I am stuck on a small problem, but been lack of logic so need your help.
I have an if statement:
func showButton() {
if viewModel.model.id == Constants.Identifiers.showId[<any>] {
//do this. Like show the button
} else {
//do that. Like hide the button
}
}
So the problem is that I want to check if viewmodel.model.id has any of the values in Constants.Identifiers.showId[]
model.id is a string, and Identifiers.showId[] is array of strings..
I know this is an easy, but... Thanks for the help!

You may use array.contains(_:) method. Documentation Link
func showButton() {
if Constants.Identifiers.showId.contains(viewModel.model.id) {
// do this. Like show the button
} else {
// do that. Like hide the button
}
}

try it
if Constants.Identifiers.showId.contains(viewModel.model.id) {
}

Related

Trying to make a game but it recognizes wrong

I am trying to make a game where you first need to make a deck with. The problem is that when people add cards that dont exist, it wont say that the card doesnt exist. I tried to fix this, but everything I tried doesnt work. I am on discord.js v12. my code is:
const cards = [
`Miner`,
`Fighter`,
`Skipper`,
`Finder`,
`Bomber`,
`Master of an Element`,
`Master of all the Elements`,
]
client.on("message", message => {
if (message.content.startsWith("card1")) {
let deckSlot1 = message.content;
deckSlot1 = deckSlot1.substring("card1".length, deckSlot1.length);
if (!deckSlot1) return message.channel.send("You didnt include a card!")
console.log(deckSlot1)
if(deckslot = cards) {
message.channel.send(`Your first card is${deckSlot1}`)
console.log("In there")
} else if(deckslot != cards) {
message.channel.send("That card doesn't exist?")
console.log("Not in there")
}
}
})
Please take a look at <Array>.includes method, you are trying to check equality (don't forget to use double equal when you want to verify an equality else it's an assignation) between an array and a string, instead you could check if the array has the string.
Exemple for your case:
if(cards.includes(deckSlot1)) {
// It's a card
else {
// It's not a card
}

Access array values - AngularJs

Hello I have array of friends this.venner
also I have array called this.convensations and output is like this.venner,
but id here is called partner_id (partner_id output is 54312, 54345, 54346 )
now I want compare if this.convensation partner_id and this.venner id if is same
something like this:
if (this.convensation in this.venner) {
//do something
} else {
// do something
}
You can use the Array.prototype.some() method.
If you wrap that in it's own checkExistsInArray function...
function checkExistsInArray(arr, compareObj){
return arr.some(function(obj){
return compareObj[Object.keys(compareObj)] === obj[Object.keys(compareObj)];
});
}
... then you should be able to use that for both arrays by passing in a custom compare object.
var friendExists = checkExistsInArray(this.venner, { id: this.friend });
var conversationExists = checkExistsInArray(this.conversations, { partner_id: this.friend });
Side Note : As others have pointed out, there are libraries out there to readily do this sort of thing. Two of the main players I'm aware of are Underscore.js and Lodash. Even if you choose not to use them, it can sometimes be helpful to see how they work under the hood when looking for a little bit of inspiration.
You can use filter,
var result = this.venner.filter(t=>t.id === '54312');
if (result.length >0) {
//do something
} else {
// do something
}
Simply you can solve this using Undescore.js _.find function
if (.find(this.friend, {"id":this.venner) {
//do something
} else {
// do something
}

How to select an option from ng repeater by text in protractor?

need to select the second option with the text "rajeshwar_9032". below is the function I wrote to click on the ng-repeat option but am unable to do so. please help.
this.SelectProfile = function(Profile){
element.all(by.repeater("profile in vm.allProfiles")).each(function (elm) {
var desiredprofile = elm.element(by.xpath('//div/div/div/div/span[contains(text(), "'+Profile+'")]'));
desiredprofile.click();
});
where the text is passed into the argument 'Profile'
You can use by.cssContainingText('<<>css>', '<<text>>') rather than such an xpath to make it more stable.
refer here for more details
You can do something like below
this.SelectProfile = function(Profile) {
return element.all(by.repeater("profile in vm.allProfiles")).filter(function(elm) {
return elm.element(by.css('.username')).getText().then(function(text) {
return text === profile;
})
}).first().element(by.css('.username')).click()
}
Finally arrived at a solution. Below code works consistently.
Thanks, #AdityaReddy I made minor tweaks to your suggestion and it worked great.Hope this helps.
this.SelectProfile = function(Profile) {
element(by.css('.icon-person-plus')).click();
element.all(by.id("select_profile_button")).filter(function(elm) {
return elm.element(by.css('.username')).getText().then(function(text) {
return text === Profile;
})
}).then(function(filteredElement){
filteredElement[0].element(by.css('.username')).click();
});
}

IE 11 - onbeforeunload event calling twice

I am using extjs. i have written code to provide warning message before redirecting other page. Warning message looks like below image
after clicking on "leave this page", same popup comes again. then after clicking on "leave this page". Here is code below:
Ext.EventManager.on(window, 'beforeunload', warnOnExit, this);// This has on document load.
//function definition
function warnOnExit(e) {
if (!bSkipChangesCheck)
{
e.browserEvent.returnValue = LY.Global.Misc.g_strWarnOnExit;
}
}
To fix this I have change "warnonexit" function as below
function warnOnExit(e) {
if (!bSkipChangesCheck)
{
e.browserEvent.returnValue =LY.Global.Misc.g_strWarnOnExit;
disableCheck();
}
}
function disableCheck() {
bSkipChangesCheck = true;
setTimeout("enableCheck()", "20");
}
function enableCheck() {
bSkipChangesCheck = false;
}
This works for me.But the solution was not accepted by higher tech persons.
i want answers of below questions.
Is timeout is only solution of this? If there are any other solutions , Please provide me
Is this the actual bug on IE11?
Need help badly!!

CakePHP 2.0 Controller render not working all of the time

I have a search controller that will look up values and render specific views according to the type of report that should be displayed. There is a weird thing happening. When I issue the $this->render the report view is not rendered. The "catch all" redirect line always is rendered... Code as follows:
public function admin_printReport() {
if (isset($this->request->data['Reports'])) {
$nons = $this->request->data['Reports'];
$res = array();
// lets lookup the noncons.....
foreach ($nons as $dat=>$vdat) {
// skip the ones that are not checked
if ($vdat == 0) {
continue;
}
// this is the temporary array that holds all of the selected report numbers > $res[] = $dat;
}
$this->loadModel('Noncon');
$this->Noncon->recursion = 0;
$results = $this->Noncon->find('all', array('conditions'=>array('Noncon.id'=>$res)));
$this->set('results', $results);
// lets do the selection now...
if (isset($this->request->data['PS'])) {
// Print summary
$this->render('summary', 'print');
} elseif (isset($this->request->data['PD'])) {
// Print detail
$this->render('detail', 'print');
} elseif (isset($this->request->data['PDH'])) {
// Print detail with history
$this->render('detailhistory', 'print');
}
}
// catch all if the render does not work....
$this->redirect(array('controller'=>'noncons', 'action'=>'search','admin'=>true));
}
Any Ideas?
I just figured it out....
for each $this->render, add return. For example:
return $this->render('summary', 'print');
I've just add a similar problem.
In a controller, that has an ajax submit, it was not submitting the render.
$this->render($viewName, 'ajax');
Using return didn't helped.
The problem was that I added
$this->render('add');
at the end of the controller, although it was not necessary since the controller name is add and the autoRender is default (true to automatically render the view with the same name of the controller).
Hope this helps someone else.

Resources