What I'm trying to do is add a check to my for loop that searches for "value_1" or "value_3". If found, than add a "#" at the beginning of that value.
Ex: #value_1, value_2, #value_3, etc
Here's my code:
for(i=1; i < columns.length-1; i++){
currentRecordKey = columns[i].dataIndex;
if(currentRecordKey == "value_1" || "value_3") {
currentRecordKey = "#" + currentRecordKey;
}
}
Worked out in my head, but this doesn't get the job done.
Any ideas?
Cheers
It should be
for(i=1; i < columns.length-1; i++){
//icon record
var currentRecordKey = columns[i].dataIndex;
if(currentRecordKey == "value_1" || currentRecordKey == "value_3") {
currentRecordKey = "+" + currentRecordKey ;
}
columns[i].dataIndex = currentRecordKey;
}
Your error is that if you don't restate that you are checking for currentRecordKey == "value_3", JavaScript will only check if currentRecordKey is true, and any non-empty string is.
Also, I've commented out icon record which might also be a typo.
Edit: also fixed the issue explained by Jasper and made currentRecordKey a local variable.
Related
I'm trying to make a website that gathers information from APIs. The following code always evaluates to 'Beep Boop Beep! I can\t find the Wikipedia page with the API! :-( \n Anyways here is more info on...'! Anyone have any ideas why?
var geoNamesWiki = result.geoNamesWiki;
for (let j = 0; j < 30; j++) {
if (geoNamesWiki.geonames[j].feature == 'country' &&
(geoNamesWiki.geonames[j].countryCode == openCage.results[0].components["ISO_3166-1_alpha-2"] ||
geoNamesWiki.geonames[j].title.includes(openCage.results[0].components.country))) {
$('#summary').html(geoNamesWiki.geonames[j].summary);
$('#wikiLink').html(geoNamesWiki.geonames[j].wikipediaUrl).attr("href", "https://" + geoNamesWiki.geonames[j].wikipediaUrl);
} else {
$('#summary').html('Beep Boop Beep! I can\t find the wikipedia page with the API! :-( \n Anyways here is more info on' + openCage.results[0].components.country + ':');
$('#wikiLink').html('https://en.wikipedia.org/wiki/' + encodeURI(openCage.results[0].components.country)).attr("href", 'https://en.wikipedia.org/wiki/' + encodeURI(openCage.results[0].components.country));
}
}
Is suspect you have a string there at var geoNamesWiki = result.geoNamesWiki;
Try parsing it to a JSON object first var geoNamesWiki = JSON.parse( result.geoNamesWiki );
I found the answer thanks to #Bekim Bacaj! I was overwriting what I had already done, so just needed to add a break on the final line of the IF part.
I am in need of some modification to my function to allow for a search of two strings on one line of a value. I am trying to work through this on my own but I need some help. Here is an example of a cell value being looked at. Assume there are no leading or trailing newlines. Also, all the cells have the same format. same number of lines, same structure of membertype: last, first etc.
Say I want to see if this cell contains a team lead with the name of last2 or a Manager with the name first4. Both the type of employee and name would be user inputted.
I tried using the following that I created with the help of this.
indexOf(':(.*):')
It returns the position of the content between and including the colons. Then I tried the following:
flatUniqArr[0].search('Supervisor:')
This is where I'm stuck. It returns the index to the last digit of the first line.
My thought was to do a search of the user inputted name between the colons if they follow the user inputted member type. How can I accomplish this?
Clarifications:
The end goal is to verify that the name and member type are on the same line and excluded from an array I am building for .setHiddenValues(). So if they are on the same line exclude from list.
Here is the function I will be adding it to:
var flatUniqArr = colValueArr.map(function(e){return e[0].toString();})
.filter(function(e,i,a){
return (a.indexOf(e) == i && !(visibleValueArr.some(function(f){
return e.search(new RegExp(f,'i')) + 1;
})));
});
return flatUniqArr;
Where flatUniqArr is the list of hidden values. colValueArr is the array of values from a column. visibleValueArr is the name which is user inputted and memberType will be the member type.
Attempts using Liora's solution: (Updated... Works now)
var flatUniqArr = []
var lines = []
Logger.log(visibleValueArr)
Logger.log(memberType)
for (var i = 0; i < colValueArr.length; i++){
lines = colValueArr[i].toString().split('\n');
var found = false;
for(var j = 0; j < lines.length; j++){
var data = lines[j].toLowerCase().split(':')
if(data[0] == memberType.toString().toLowerCase() && data[1].indexOf(visibleValueArr.toString().toLowerCase()) != -1){
found = true;
}
}
Logger.log(found)
if(found == false){flatUniqArr.push(colValueArr[i])}
}
return flatUniqArr;
It works now. It seems like a lot of code though. I'd be open to alternative solutions if they are faster and/or less lines of code.
Updated: Added .toString().toLowerCase() as the user may input lowercase values.
I assume all the line have this format.
If you split each line with the separator ":"
var array = value.split(":")
Then you'd have
array[0] //the current role
array[1] //the list of name
array[2] //the email
And you can check each names then
if(array[0] == "Team Lead" && array[1].indexOf("last2") != -1)
An example with a linesplit:
var lines = value.toString().split("\n");
var found = false;
for(var i = 0; i < lines.length ; i++){
var data = value.split(":")
if(data[0] == "Team Lead" && data[1].indexOf("last2") != -1){
found = true;
}
}
How about just building the regex using the user input?
function search(line, employeeType, employeeName) {
var regexp = '/' + employeeType + ': ' + employeeName + '/'
return line.search(regexp)
}
Or better yet, if it always occurs at the beginning of the string, just use startsWith()
for(var i = 0; i < textList.length; i++){
for(var j = 0; j < titles.length; j++){
if(textList[i] === titles[j]){
console.log ("it includes my " + titles[j] + ' the match is ' +textList[i] + " counter " + i)
}
}
}
this is my code, but it won't return a match. I tried == and ===. But when i tested .includes() it worked. Can someone explain what's happening ?
If you are sure that all elements are in type String, you can use the method .search():
Prototype search
It will return the position of the match, if it dosent match in any position you will get -1 as return, soo > 0 it match.
I just tested your code with a very basic test example as follows:
let textList = ['book1', 'book2','book3']
let titles = ['book', ' tester', 'not_this', 'book2']
for(var i=0; i<textList.length;i++){
for(var j=0; j<titles.length;j++){
if (textList[i] === titles[j]){
console.log ("it includes my " + titles[j] + ' the match is ' +textList[i] + " counter " + i)
}
}
}
And I got the expected result it includes my book2 the match is book2 counter 1 so with this specific code I would suggest looking at you input arrays.
With regards to your question regarding why .includes() works and this doesn't, again we would need to se your input arrays but I would hazard a guess that it is something to do with the type checking within this function.
Finally, as others have suggested, there are other (more succinct) ways of achieving this with built in array functions, however your original question was about why this code in particular doesn't work so I've left these out.
I'm making a Ukrainian phonetic keyboard. People type in English, and the letters automatically change to the corresponding Ukrainian characters. However, when I'm using it, I sometimes need to write myself notes in English (they serve the same purpose as comments in code- for other people and for myself).
I'd like to indicate the start of a comment with a tag ("<"). How can I check if there's currently an open tag?
I'm thinking something like this:
if (number of "<" is greater than ">") {//if a tag has been opened and not closed
//disable translation, type in English
}
I understand how to disable the translation- however, I'm unsure about the
"if"
How can I check if
number of "<" is greater than ">"
Thanks!
You can count number of specific characters using .match()
In your case
var string = "<<<>>";
if ((string.match(/</g)||[]).length > (string.match(/>/g)||[]).length) {
console.log("More");
}
else {
console.log("Less or equal");
}
counting each of them is like below
var countGreaterThan = (temp1.match(/</g) || []).length;
var countLessThan = (temp1.match(/</g) || []).length;
and temp is the string value of the textarea
Depending on where your data is, you can do:
var data = document.querySelector('#data-container').innerHTML;
var isOpenTagPresent = getSymbolCount('<') > getSymbolCount('<');
if(isOpenTagPresent) {
//execute your logic
}
function getSymbolCount(symbol) {
var count = 0;
for(var i = 0; i < data.length; ++i) {
if(data[i] === symbol) {
count++;
}
}
return count;
}
Hope this helps, cheers!
I have dropdownlist with following html code that I grabbed from debug window. It is a code in blackbox that I can't get to. In this webpage I want to use javascript to remove the options if it does not start with 'SS '. How do I do that?
<select id="prgid" special="lists.specprog" name="UF-003054-1">
<option value=""> </option>
<option value="5488">SS Twain CE</option>
<option value="5487">Twain IS</option>
</Select>
I am doing something like this which is not working. Please help.
var select=document.getElementById('prgid');
for (i=0;i<select.length; i++) {
var prg = select.options[i].value;
if (!prg.substring,0,3) == 'SS ') {
select.remove(i);
}
}
You had the right idea. Try this:
var select = document.getElementById('prgid');
for (var i in select.children) {
if (select.children[i].innerHTML.match('SS') !== 0) {
select.children[i].outerHTML = '';
}
}
http://jsfiddle.net/howderek/35vagg9u/
var select=document.getElementById('prgid');
for (i=0;i<select.length; i++) {
var prg = select.options[i].text;
if (prg.substring(0,3) == 'SS ') {
select.remove(i);
}
}
Use the above javascript.
you have to use select.options[i].text to get option text. But if you use .value that will give you the option value. And see the syntax of .substring()
For code reference, see jsFiddle
Well, howderek already posted a good working alternative, but it may be worth posting a fixed version of your code for a better understanding of what went wrong. You were pretty close!
There were just a couple of missteps:
The condition within the if statement:
!prg.substring,0,3) == 'SS '
should instead have been:
prg.substring(0,3) != 'SS '
You were calling the substring() function incorrectly, and furthermore your placement of the ! operator would have caused you to compare a casted boolean against a string, rather than a string against a string (which is what you would want in this case).
Additionally, since you're actually accessing the text of the options, and not their values, you should use select.options[i].text; rather than select.options[i].value;.
Your resultant JavaScript would thus look like:
var select = document.getElementById('prgid');
for (i = 0; i < select.length; i++) {
var prg = select.options[i].text;
if (prg.substring(0, 3) != 'SS ') {
select.remove(i);
}
}
Here's a JSFiddle to demonstrate - note how all options that do not start with "SS " have been removed.
Hope this helps! Let me know if you have any questions.
You're close, you just missed a couple things
var prg = select.options[i].value;
//should be
var prg = select.options[i].text;
Should use .text, because .value will return 5488 and 5487, .text will return "SS Twain CE" And "Twain IS"
And your substring method wasn't formatted correctly, it should have been
if (prg.substring(0, 3) != 'SS ')
Putting it all together
var select=document.getElementById('prgid');
for (i=0;i<select.length; i++) {
var prg = select.options[i].text;
if (prg.substring(0, 3) != 'SS ') {
select.remove(i);
}
}