I have an array which I captured it from google spreadsheet.
arr = [abc#gmail.com, xyz#gmail.com, pqr123#gmail.com.....]
Every other match is giving 0. however, I have a value which is pqr123#gmail.com which is throwing result -1. I realized that it is alpha numeric and that could be the reason to not match. but again if the array element is alphanumeric it should match. what is the solution?
I am using the following code to match :-
var arr = sheet2.getRange(4,1,sheet.getLastRow(),1).getValues();
var match = arr[0].indexOf(eRecord.email) //eRecord.email is 'pqr123#gmail.com'
Logger.log(match) //current result -1
In your script, you use getRange(4,1,sheet.getLastRow(),1) of var arr = sheet2.getRange(4,1,sheet.getLastRow(),1).getValues(); as the range. In this case, the values are retrieved from a column. From this situation, I would like to propose the following modification.
From:
var match = arr[0].indexOf(eRecord.email)
To:
var match = arr.flat().indexOf(eRecord.email);
By this modification, 2 dimensional array with 1 dimensional array which has one element are flatten, and the returned value is the row index.
Reference:
flat()
I think that the problem could be in arr[0] or eRecord.email. Please, doublecheck that arr[0] is really what you need and eRecord.email contains email.
Related
I have the following structure, either a single string array ['stufff sjktasjtjser ((matchthis))'], or the same structure in a nested array ['stufff', ['more stuff ((matchhere))'], '((andanother))'];
I can loop and match all regex in the brackets and even replace the text:
//after flattening the array lets take the first one, assume I am looping in the first element.
var matches = currentArrayElement.matchAll('fancyregex') //pretend I am matching the brackets
matchs.forEach(match=>currentArrayElement=currentArrayElement.replaceAll(match[0],'whatwhat'))
console.log(currentArrayElement)//'stufff sjktasjtjser whatwhat'
//but what I actually want is
// currentArrayElement = ['stufff sjktasjtjser','whatwhat'];
Does anyone knows how I can achieve that? Or any template lib that can do that within nested arrays? I need to output sometimes an array of a string ['tss'] and sometimes an array with an object [{}].
Thanks.
The issue was that I needed to change the array in that index not the entire array.
Here is what I did then:
//after flattening the array lets take the first one, assume I am looping in the first element.
var matches = currentArrayElement.matchAll('fancyregex') //pretend I am matching the brackets
matches.forEach((match) => {
currentArrayElement[i] = c.split(match[0]).flatMap(
(value, index, array) => (array.length - 1 !== index
? [value, 'whatwhat',]
: value),
);
});
Suppose I have an Javascript array,
var example = [and, there,sharma<br, />, ok, grt]
Now I want to randomly delete some array values - but not those values which have
<br
in them, in the above example, I want to make sure
"sharma<br" is not deleted.
I also do not want to delete "/>".
Can anyone help me. I would really appreciate the answer.
First of all, that is not a valid array, unless you are missing the string quotes. Anyway, what you are searching for is Array.Filter. In your case :
var filtered = example.filter(v => v.indexOf("<br") != -1 || v.indexOf("/>") != -1)
If I have understood the problem correctly, then you want to keep those entries in the array which have substrings "<br" and "/>"
If thats the case, you can try using javascript string method includes() to see if a string contains a particular substring.
JavaScript array must be created as below
var example = ["and"," there"",sharma<br","/>","ok"," grt"];
Using splice method , to delete the array values specifying the index positions.
array splice() method changes the content of an array, adding new elements while removing old elements.
Syntax
Its syntax is as follows −
array.splice(index, howMany, [element1][, ..., elementN]);
Parameter Details
index −
Index at which to start changing the array.
howMany −
An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed.
element1, ..., elementN −
The elements to add to the array. If you don't specify any elements, splice simply removes the elements from the array.
Return Value
Returns the extracted array based on the passed parameters.
var removed = arr.splice(2, 2);
This would remove your suggested output to be my assumption .
function getFileExtension(i) {
if (i.indexOf(".") < 0) {
return false;
}
var filenameParts = i.split(".");
return filenameParts[filenameParts.length-1];
}
Here's the whole code. I understand it all except for the last line. I know what it does, but I don't know how or why. The second to last line splits the string at the ".", and then how does the last line actually get all the letters on the right side of the string?
By calling var filenameParts = i.split("."); an array is created containing the different parts. Imagine we use the filename test.txt and we use that string to split, we'll get an array like so:
filenameParts = ["test", "txt"]
Because the index of the first item in an array is 0, and we need the last item in the array, we call filenameParts.length-1 to get to the last item.
More information about javascript arrays can be found here.
The .split() function returns an array of strings, not a string. The expression filenameParts[filenameParts - 1] fetches the last element of the array.
filenameParts.length delivers the count of the filenameparts, split in the line above. filenameParts[number] delivers the one item of the array, which is positioned at number. -1 because arrays start at 0 not at 1. So it delivers the last item of the array. Clear?
filenameParts is an array and you read a single value with it's index. A value in this case is one part of the string between the ".".
filenameParts.length is equal to the count of values inside the array. As an array index starts with 0 you have to subtract 1 to get the index of the last value.
It looks like your function getFileExtension is designed to return the file extension of a given file. For example getFileExtension('image.gif') would return gif.
In the line (given that i is set to image.gif):
var filenameParts = i.split(".");
filenameParts will be an array, where image.gif has been split on the period. So filenameParts = ['image', 'gif'] where element zero is image and element one is gif. Remember that array indices are zero-based!
In the last line:
return filenameParts[filenameParts.length-1];
the function itself will return the last element in the filenameParts array (['image', 'gif']) which is gif. The part filenameParts.length-1 says get the length of the filenameParts array (which is 2), subtract 1 (which is 1), and return that element of the filenameParts array. So we return filenameParts[1] which is the last element of the array (remember, array indices are zero-based).
To get the last element of the array we could also have done
return filenameParts.pop();
because the pop() function returns the last element of an array.
var filenameParts = i.split('.') returns an array of made of the splitted elements of i
filenameParts[filenameParts.length-1];
select the last element of that array
I have this code that fetches data and puts it into an array:
this.$httpGetTest(this.test.testId)
.success(function (data: ITestQuestion[]) {
self.test.qs = data;
});
It works and populates the array starting with self.test.qs[0].
However many times my code references this array (which contains a list of questions 1...x)
I must always remember to subract 1 from the question number and so my code does not look clear. Is there a way that I could place an entry ahead of all the others in the array so that:
self.test.qs[0] is null
self.test.qs[1] references the first real data for question number 1.
Ideally I would like to do this by putting something after the self.test.qs = and before data.
Push values at start of array via unshift
self.test.qs.unshift(null);
You need to use Splice(), It works like:
The splice() method changes the content of an array, adding new elements while removing old elements.
so try:
self.test.qs.splice(0, 0, null);
Here mid argument 0 is to set no elements to remove from array so it will insert null at zero and move all other elements.
Here is demo:
var arr = [];
arr[0] = "Hello";
arr[1] = "Friend";
alert(arr.join());
arr.splice(1,0,"my");
alert(arr.join());
You can start off with an array with a null value in it, then concat the questions array to it.
var arr = [null];
arr = arr.concat(data);
You could do something like:
x = [null].concat([1, 2, 3]);
Though there isn't anything wrong with doing something like:
x[index-1]
I'd prefer it to be honest, otherwise someone might assume that the index value returned is 0 based.
I have an assignment for a Javascript course where I have to count how many of each specific types of elements occur in an array. The array is 105 elements long, and just occurrences of the numbers 1, 2, 3, 4, and 5. I have to count how many 1's, 2's, 3's, etc.
Of course there's a simple way to do this using a loop however my teacher has added the following at the end of the assignment:
Use only the length property, toString(), sort() and indexOf() methods. Please no loops or conditional statements.
I have no idea how to do this assignment without using a loop. Any help you can give me is greatly appreciated. Thanks!
this will be the answere:
var c=[1,2,3,4,5,2,3,4,5]
c.sort()
cout_of_1 = c.indexOf(2)-c.indexOf(1);
cout_of_2 = c.indexOf(3)-c.indexOf(2);
cout_of_3 = c.indexOf(4)-c.indexOf(3);
cout_of_4 = c.indexOf(5)-c.indexOf(4);
cout_of_5 = c.length-c.indexOf(5)+1;
Since this is an assignment and you haven't really shown an attempt, I'll just give you a couple of hints.
Once you sort the array, all of the 1s will be grouped together at the start of the array, then all the 2s, etc.
The indexOf function returns the first index at which a given element can be found in the array, or -1 if it is not present.
Given the index of the first occurrence of each digit in a sorted array, you should be able to calculate how many of each digit there are. I'm not sure you'll need toString() at all. Your instructor might have thrown that in to be sneaky.
First, sort the array, then grab the first occurrence of the next value. That's the amount of numbers you have, minus the ones you already have.
var array = [1,2,5,3,2,4,5,2,1,3,3,4,5,6,2];
array = array.sort();
var ones = array.indexOf(2);
var twos = array.indexOf(3) - ones;
var threes = array.indexOf(4) - ones - twos;
...
However for the last value (5) there is no next value to check. You can work around this by checking the length and subtracting one from it.
var fives = array.length - ones - twos - threes - fours;
Keep in mind that this is actually bad code, and you should use loops.