Conitnuous looping for while loop [duplicate] - javascript

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
How to find if an array contains a specific string in JavaScript/jQuery?
(7 answers)
Closed 7 months ago.
while (true) {
if (memberName == subMemberGroup.indexOf("Leonardo", "Catherine", "Luther", "Bruce", "Amy") !== -1) {
console.log("\nMember's name exists in database. Please enter a new name.");
var memberName = input.question("Please enter member's name: ");
} else break;
}
I am trying to loop through to see if "memberName" that the user has input matches any of the following strings in the "subMemberGroup" array.
However, when I tried using while loop, it keeps saying that Member's name exist even thought the "memberName" that the user input does not match the strings in "subMemberGroup" array. I tried using if statement with a break but it still does not work.
The reason why I used while loop is because I want my code to re-prompt the user the enter a new name if the "memberName" matches the strings in "subMemberGroup" array.

Related

How to set multiple filter criteria in the script? [duplicate]

This question already has answers here:
Need help filtering an array in JavaScript
(2 answers)
How to use OR condition in a JavaScript IF statement?
(8 answers)
Closed 6 months ago.
I have a set of data with location info. I am trying to filter only the data where Location matches 'India' OR 'US'. I am able to match a single criteria using the below:
var data = ws.getRange("A2:F" + ws.getLastRow()).getValues();
data = data.filter(function(r){ return r[location] == 'India'});
However, for multiple criteria, the below is what I have tried.
var data = ws.getRange("A2:F" + ws.getLastRow()).getValues();
data = data.filter(function(r){ return r[location] == '=OR("India","Singapore")'});
This does not return any error nor any result. Pls guide.

Remove row if value equal to [duplicate]

This question already has answers here:
Looping through array and removing items, without breaking for loop
(17 answers)
Remove items from array with splice in for loop [duplicate]
(5 answers)
Closed 2 years ago.
I am trying to remove all the rows that don't have the value '2A' or '2B' but whenever I execute the code it deletes every row except the first one.
Here is my bit of code :
function supprimerColonnesInutiles() {
for(i = 1; i < data.length; i++) {
if(data[i][13] === '2019-09-30'){
data.splice(i);
}
if(data[i][2] !== '2A'){
data.splice(i);
}
}
}
This code works perfectly to delete every row with the 2019 date but fails to do what I expect it to do to delete rows that contain 2A as a value (i'd like to keep 2B values as well).
here is the csv file that serves as my array, as you can see the second column contains 2As and 2Bs but also number from 1 to 100.
This is what I get if I execute the code enter image description here
and here is my csv/array enter image description here
My question is what could be causing this issue and how can I fix it ?

How do I create an if statement that sets an individual variable equal to at least one of my array elements [duplicate]

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 3 years ago.
I have a line of code that is supposed to act as a check for if the user inputs a value that isn't within my array.
I made an array with all valid inputs and then made and if statement that checks this.
var productCode = ["LT","ST","DC","LC","PR","SP"];
var productChosen = prompt("Choose a product code, LT, ST, DC, LC, PR, or SP");
if ( productChosen === productCode) {
etc..
}
else {
alert("Please input a valid product code");
}
It always goes to the else statement.
The productChosen is a string. The productCode is an array. These types can never be equal. What you are looking for is includes() to check if the string is included in the array.
if(productCode.includes(productChosen)) { ... }

finding a index of string [duplicate]

This question already has answers here:
How to check if the URL contains a given string?
(21 answers)
Closed 6 years ago.
I have a long array of strings with page IDs; when the current page ID matches one from the array, stuff needs to happen (alert in this test).
The alert pops up on any pages, regardless if the url contains one of the ids from the array or not. What is wrong with my if statement: if(pageHref.indexOf(id))? thanks for any advise
var pageHref = window.location.href;
var ids = ['14528','14417','17529'];
for (var i = 0; i < ids.length; i++) {
var id = ids[i];
if (id.length > 0) {
if(pageHref.indexOf(id)){
//do something
alert('a');
}
}
}
A blog post here has a table that explains what values evaluate as true in javascript conditional statements. For numerical values it states only +0,-0 and NaN evaluate to false. Because indexOf returns -1 for no match, it is evaluating to true for non-matching ids. You would want to use:
if(pageHref.indexOf(id) > -1)
IndexOf returns a number, if the number is -1, there is no match.
Try for example.
pageHref.indexOf(id) != -1.

get the object's attribute value in javascript [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
I am going to create an array called idArray which contains Id of students in a csv file.
inputData is an array containing 10 objects(records of csv). Each object is an array and have a "Unique Id". I would like to check if I already have this "Unique Id" in my idArray, if no, add it to the array and if yes go to the next record.
getIds: function (inputData) {
inputData.forEach(function () {
if(idArray.length!=0) {
idArray.forEach(function () {
if (inputData."Unique Id"!= idArray)
idArray.push(inputData."Unique Id");
});
}
else
idArray.push(inputData."Unique Id");
});
return idArray;
}
I know that inputData."Unique Id" is wrong, but how can I get Unique Id?
Thanks
Try this
if (inputData["Unique Id"] != idArray)
If the attribute name doesn't have any spaces or special characters in it you can do
object.attributeName
Otherwise you need to reference it inside square brackets
object["attribute Name"]

Categories

Resources