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.
Related
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.
I am new to JS. I have task:
Given names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for is not found, print Not found instead.
Note: Your phone book should be a Dictionary/Map/HashMap data structure.
Input Format
The first line contains an integer, , denoting the number of entries in the phone book.
Each of the subsequent lines describes an entry in the form of space-separated values on a single line. The first value is a friend's name, and the second value is an -digit phone number.
After the lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a to look up, and you must continue reading lines until there is no more input.
Note: Names consist of lowercase English alphabetic letters and are first names only.
Here is my code. But i cant check name exists in array or not. Js includes didnt work?
function processData(input) {
//Enter your code here
const n = parseInt(input);
const d = [];
for (var i = 1; i <= n; i++){
var line = input.split('\n').splice(i,1);
let x = line[0].split(" ");
d[x[0]] = x[1];
}
console.log(d)
let m = n;
while (true){
try{
name = input.split('\n').splice(m+1,1);
if ( d.includes(name)){
console.log(name,'=',d[name])
} else console.log('Not found')
m +=1;
}
catch(err){
break
}
}
}
name is the result of a splice on an array, which means it's an array, but you've filled d with strings, so d.includes(name) is looking for a newly-created array inside an array of strings. No array is ever equal to a string (without conversion, which includes doesn't do).
If you meant name to be the entry you removed from the array with splice (since it returns an array of removed entries), you'd need to use [0] on the end to get it:
name = input.split('\n').splice(m+1,1)[0];
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^
I haven't gone through the code to look for other issues, but that will at least look for a string in the array of strings rather than looking for an array in the array of strings.
Side note: You appear to be relying on getting an error to stop your loop, but beware that accessing beyond the end of an array is not an error in JavaScript, you just get the value undefined.
Beginner
I have 3 arrays, i would like to combine and sort, according to date and time.
All 3 arrays, only have date and time values.
Example:
var slackStart = []; //time the current starts for the week.
var CurrentTurn = []; //time the current turns for the week.
var slackStop = []; //time the current stops for the week.
Output slackStart (contains all starting times for the week):
[ "2017-10-24T03:15:36Z", "2017-10-24T09:13:44Z", "2017-10-24T15:41:27Z", "2017-10-24T21:40:27Z", "2017-10-25T03:47:20Z"]
I need to combine the 3 arrays and sort according to date and time. I also need to know from which array the value came. If its slackStart, i know its the starting time for the current.
I want to display it like this.
Current start: 11/11/17 12:00
Current turn: 11/11/17 14:00
Current Stop: 11/11/17 17:00
Combining the arrays is easy but how can i know from which array it came?
I tried using keys but struggled a bit.
Any help would be appreciated.
You can combine arrays simply by calling the 'concat' method:
var aryA = [1,2,3]
,aryB = [4,5,6]
,aryResult = aryA.concat(aryB);
'aryResult' will contain [1,2,3,4,5,6], in terms of identifying where it came from, are the dates going to be unique?
If not then you should forget an array and convert to an object with unique key for each entry, then you can simply use the key.
var objResult = {};
for( var idx in aryResult ) {
objResult["k" + idx] = aryResult[idx];
}
The above will translate the combined array into an object with each member having a key that starts with k followed by the array index.
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
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);