I have the following snippet within my react component that passes in a transaction object with several fields. Why does it not work?
I'm trying to only show a value formatted with a $ sign if the value is not null.
${!isNaN(transaction.debit) ?
parseFloat(transaction.debit).toFixed(2) : null}
Is there a better way to do this? My array of data basically has debit fields that may not have a value and in that case I just don't want to display anything in the cell.
How about concatenating the string '$' to the beginning of your float if the transaction.debit value is Truthy or it is 0 (assuming we want to show a value for 0) - otherwise show an empty string.
{transaction.debit || transaction.debit === 0 ? '$' + parseFloat(transaction.debit).toFixed(2) : ''}
Related
Short version: I want to write a check that says if this piece of data from the GraphQL query is undefined, set it to 1 week ago. How do I write this in the return in a typescript file? This is what I have been trying:
{data?.program?.flagPageVisits?.edges?.[1]?.node?.createdAt === undefined ? (data.program.flagPageVisits.edges.[1].node.createdAt = new Date(moment().subtract("days", 7).format("DD-MMM-YYYY"))) : ""}
I created a new table LastPageVisits and I only want to show rows in a table with a red dot if it's a "new" row for the user, meaning it was created since their LastPageVisit. If the value of the (second to last) LastPageVisit is undefined, I want to set it to 1 week ago. How can I write this check in a React/Typescript return function (data is coming from GraphQL query)?
I am trying make a map that filters markers based on certain parameters. initially I have 2 dropdown filter and one input filter. the input filter numbers only.
my Array looks like :
['Belmont', 42.4135648, -83.1775028, 'A', '48227, 48235, 48219, 48223', 'Female', 'veteran'],
now I would like to simplify the work by combining my parameters all together, my new array will look similar to below:
['Belmont', 42.4135648, -83.1775028, 'A', '48227, 48235, 48219, 48223 , Female ,veteran'],
I get all these filters to work separate, example as below.
https://jsfiddle.net/gf9o3k0q/2/
so I have my code:
if (
(gender == "All" || marker.gender == gender) &&
(people == "All" || marker.people == people) &&
(serviceSearchTerm !== null && serviceSearchRegx.test(marker.service))
) visible = true;
change to only:
if (
(serviceSearchTerm !== null && serviceSearchRegx.test(marker.service))
) visible = true;
nothing shows after my type in any of the values. I would like users to only enter one the values in that parameter.
such as they only type in Female, or Veteran, or 48337 for that point to show.
any help would be appreciated
The actual issue explained in how do I filter parameter with text and number in Google Map api can be easily solved by making a case insensitive string comparison.
Check out this fiddle: http://jsfiddle.net/adgLr4ot
Type either "female" or "Female" or "FEMALE". All will work, because toUpperCase() makes both document.getElementById("zipcode_filter").value and marker.service uppercase values that are deemed equal. Hence the markers can be filtered regardless of user's input case.
See related SO question to learn more: How to do case insensitive string comparison?
Keep up the good work and your learning journey!
I have a situation where a user can type a ZIP code value in order to filter some data. I also have this filter set up so that the last values are kept, so that if the user re-enables the filter, those values are available to immediately filter the data again when the filter is re-enabled.
The problem I'm running into is that my filter value is being passed even when the filter's been cleared out. Because this is an array of values passed to Mongoose/MongoDB from our Angular app via the body of a POST request, what effectively get's filtered on is ['']. And that returns me zero results - not surprsingly.
So I am trying to come up with some conditional checks to prevent the filter from firing when the result is ['']. I can't simply exclude based on array.length, because even this value is considered an array with a length of 1 - which would also be the case if an actual zip code were there ['77799']. This is what I've tried doing:
if ( zipArray !== [] && zipArray !== [''] && zipArray[0] !== '') {
console.log(zipArray);
console.log(zipArray.length);
this.sendZipcode.emit(zipArray);
}
What else could I try here to exclude emitting the value when zipArray === [''], that would still accept an array with a value like ['77799']? Can I check on a minimum number of characters in whatever element is present in the array? Suggestions?
You can remove all the undesired values from the array using filter and then check if you still have valid zipcodes to send to your server:
let zipArrayWithNoEmptyZip = zipArray.filter(zip => zip.length > 0);
if (zipArrayWithNoEmptyZip.length > 0) {
console.log(zipArrayWithNoEmptyZip);
console.log(zipArrayWithNoEmptyZip.length);
this.sendZipcode.emit(zipArrayWithNoEmptyZip);
}
In Javascript I have a collection of objects, whose values I'm storing in variable
var filters = {
BeginDate: $("#BeginDateRange").val(),
EndDate: $("#EndDateRange").val(),
ListOfCodes: $("#ListOfCodes").val(),
//ListOfCodes: $("#ListOfCodes").val().join(),
...
}
Based on where I use the collection, some of its objects remain 'undefined', and it is intended.
ListOfCodes above is an array of string values, and I want to pass it to the binder as a single comma-separated string (e.g ["1"], ["2"] -> "1,2")
I was able to make a use of .join(), and it worked successfully. However, I found later that the code will crash if the .join() does not have a value to join.
Is there a way to apply .join() INSIDE the collection to the variable ONLY if it has value? Something like
var filters = {
BeginDate: $("#BeginDateRange").val(),
EndDate: $("#EndDateRange").val(),
ListOfCodes: if( $("#ListOfCodes").val() )
{$("#ListOfCodes").val().join()}
else
{$("#ListOfCodes").val()} //value remains undefined
,
...
}
EDIT: I ask about the possibility of applying .join() method inside the collection, not checking for empty values.
Just moving this as an answer.
What about a ternary statement?
ListOfCodes: ($("#ListOfCodes").val()) ? $("#ListOfCodes").val().join() : null
I'm trying to return the minimum value in a series of numeric input fields. When the fields are blank, they are being read as 0.
Math.min is returning 0 unless all fields are populated with values.
See below code, I'd appreciate any help on this.
if ($H8M1$ > 0)
{
return(
Math.min(
$H8M1$,
$H8M2$,
$H8M3$,
$H8M4$,
$H8M5$,
$H8M6$,
$H8M7$,
$H8M8$,
$H8M9$,
$H8M10$,
$H8M11$,
$H8M12$,
$H8M13$,
$H8M14$,
$H8M15$,
$H8M16$,
$H8M17$,
$H8M18$,
$H8M19$,
$H8M20$));
}
An empty field in javascript is a falsy value and in this case the parser force the coercion to a number that is 0 (still a falsy value).
you could remove all the empty fields from the calculation or loop over the series and assign the empty elements to the minimum value of the series but in this way your already get the minimum value thus the Math.min is not useful anymore.
For each value you are passing to Math.min(), assign 0 to the value if that is blank.
if($H8M1$ == "")
$H8M1$ = 0;