Display specific parts of array - javascript

This code is creating an array, dateArray, from the parameter, date3, which is being passed through a function.
The data being passed through that function is a full date in the format, "12312015". The variable month should break off the first two characters of the array, dateArray. Then the variable Smonth converts the month array back into a string. The last line is then supposed to display the string "12" through the HTML form in a textbox. When the button on the form is pressed the function runs but it displays nothing.
var dateArray = [date3];
var month = dateArray.slice(1, 2);
var Smonth = month.toString();
VerifyForm.dobBox.value = Smonth;

The problem is you are creating an array dateArray with only 1 item in it which is the date string, so slicing it from 1 to 2 will return an empty array not the first and second characters of the original string.
Since date3 is a string, you can use String.substring() to extract the first 2 characters
var month = date3.substring(0, 2);
VerifyForm.dobBox.value = month;

After this
dateArray = [date3];
the dateArray contains single element at index 0. And here
dateArray.slice(1, 2);
you are trying to get range from 1 to 2 elements. But they are not there.
So you are getting nothing - empty array.

The dateArray you're creating is an array with a SINGLE value.
the slice function you're using is used to 'slice' up arrays with MULTIPLE values.
To achieve what you're trying to achieve you need to use substring.
Example:
VerifyForm.dobBox.value = date3.substring(0,2);

Related

javascript Calculating moment subtraction

I want to subtract the ones formatted by one and two respectively.
ones and twos are json values ​​and moment was applied.
But in substract , an error occurs saying there is no string in substract.
const one = ones.format("YYYY-MM-DD HH:MM:SS"); // 2023-01-18 22:01:42
const two = twos.format("YYYY-MM-DD HH:MM:SS"); // 2023-01-16 05:01:21
const result = one.subtract(two);
I want to subtract one and two values ​​from each other and put them into a variable. How should I handle it?

Replacing array value with a matching object

I'm building a react app to display esports matches from the pandascore api. I've pulled the dates of all of the matches and created a multi-layer array that groups them in to weeks.
I'm trying to replace the dates in the array that I've made with the corresponding match. This way, I can have an object with matches grouped by week.
// map an array of dates
//getting dates that we will replace
// ie ["2018-07-29T20:41:28Z", "2018-07-29T21:32:14Z", "2018-08-05T01:08:11Z"]
Object.values(groups).map((matchDateTime) => {
console.log(matchDateTime);
//loop over every date in the array
for (let d = 0; d < matchDateTime.length; d++) {
//set a variable and set it to find a match from the matches array
//that has the same date and time to the
//entry in the matchDateTime array
let matchWithDate = matches.reduce( match => {
//if dates match, then we will replace the date in the
//matchDateTime array to the match object we found
//this is the logic I'm looking for but doesn't return anything
match.begin_at = matchDateTime[d] ? matchDateTime[d] = match : 'no match';
//returns the correct date - line 71 in the screenshot
console.log(matchDateTime[d]);
//returns the correct match - line 72
console.log(match);
// returns the coresponding match date/time - line 73
console.log(match.begin_at);
}
);
}
// console.log(groups);
});
What I'm getting is an error after the loop runs a few times. There shouldn't be any dates/times that are null or undefined as I've pulled them directly from the objects I'm checking against.
I feel like I'm close. Any help is appreciated!
I altered my loop to use the filter method instead of reduce. I also edited my ternary operation to regular if logic.

How to match alpha numeric elements with array javascript

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.

How does this line " return fileNameParts[fileNameParts.length-1]; " work?

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

javascript for loop and array pushing [duplicate]

This question already has answers here:
Array.push() makes all elements the same when pushing an object [duplicate]
(2 answers)
Dates changing when I change another date [duplicate]
(1 answer)
Closed 26 days ago.
I'm trying to create an array of date objects starting from a certain date up till today.
Here's the code I have:
var beginning = new Date("04,06,2013");
var dates = [];
var today = new Date();
while (beginning < today){
var x = beginning;
console.log(x);
dates.push(x);
beginning.setDate(beginning.getDate()+1)
}
for (var i in dates) {
console.log(dates[i]);
}
In the while loop I see the correct dates incrementing but when I print out the dates in the array at the last for loop I see all the dates that are pushed being today's date.
Any ideas?
What your code does is push a whole bunch of references to the exact same Date object. So, you have an array full of all the same Date object and each time you change that object, all elements in the array just point to the same object so they will all appear to change.
When you push an object into an array or assign an object to a variable, it does not make a copy, it pushes a reference to it (think of it like a pointer in other languages). To push different date objects for each iteration of the loop, you'd have to create a new date object each time through the loop and push that.
In javascript, assigning an object or an array to any variable (which includes pushing it into an array) only assigns a reference to that object or array, not a copy. This is a common issue that bits most people coming up to speed on javascript.
You can make a new date object each time through the loop like this:
var beginning = new Date("04,06,2013");
var dates = [];
var today = new Date(), x;
while (beginning < today){
x = new Date(beginning.getTime());
console.log(x);
dates.push(x);
beginning.setDate(beginning.getDate()+1)
}
You're only working with one single Date instance throughout all that code.
To create a copy of a Date, do this:
x = new Date(beginning.getTime());
Then call the .setDate() method to move it forward.
The setters on JavaScript Date instances change the object. They don't create a new one.

Categories