Nested Ternary Operator - javascript

Having some issue with nested ternary :
I have code :
var genericName = Beauty;
setTitle = (!empty(shopOnline) ? shopOnline + ' | ' : '')
+ (!empty(productName) ? productName + ' | ' : '')
+ genericName;
I want scenario like:
if genericName is not empty - Shop Online | Product Name | Generic Name
if genericName is empty - Shop Online | Product Name
Currently I am getting the output of my code (if genericName is empty) - Shop Online | Product Name |
How we can add the nested ternary operator to remove " | " if genericName is empty after Product Name
( (!empty(productName) ? productName + ' | ' : '') )

Using filter() and join()
[shopOnline, productName, genericName].filter(Boolean).join(' | ')
Here Boolean function is given as a callback to filter out the falsy values like
0, -0, null, false, NaN, undefined, ''
You can also use your empty() as filter's callback
[shopOnline, productName, genericName].filter(i => !empty(i)).join(' | ')

Related

Display initials of name on condition angular

I have this data in variable
Names : "Amit Singh, Kumar Anand"
Names : "Ashish Singh"
this can be single name or multiple names seperated by commas "James, Anand, xyz,..."
In for loop
<div *ngFor="let user of Info">
{{ (user.Names != null && user.Names.length>0) ?
(user.Names |
slice:0:1)
: '' }}
</div>
Here we get output only A but i want AS if comma is not present otherwise if comma is present (i.e. multiple names are there) i want to display M in place of first name and last name
Any solution Thanks
You can do the following if you want it in 1 line;
user.Names.split(",").length === 1 ?
user.Names.trim().split(" ").map(x => x[0])
.reduce((acc, curr) => acc + curr)
: 'M'

Updating PostgreSQL data in javascript where ajax URL contains a single quote

Modifying data in PostgreSQL in javascript where URL contains a single quote
What would be the best way to approach this problem?
In some of these approaches, success has been printed out but there is no change in the database.
In the case where there is no single quote success would print and the database will update as well.
Attempts
first_name = D'Angelo
first_name = D%27Angelo
first_name = D\'Angelo
first_name = 'D''Angelo'
first_name = D\\\'Angelo
database
_______________________________
|first_name|last_name|is_present|
---------------------------------
| D'Angelo |Williams | false |
---------------------------------
| Georgia | Fritz | false |
---------------------------------
| Luca | O'Keefe | false |
_______________________________
file.js
modify_student_data = "D'Angelo: Williams :false" //this attempt will work for the row that contains Georgia
first_name = "D'Angelo";
last_name = "Williams";
is_present = "true";
$.ajax({ url:'/data/student_attendance/' + modify_student_data + "*" + first_name + ':' + last_name + ':'+ is_present,
success: function (result){
console.log("Success");
}
error: function (er){
console.log("error");
}
});
desired outcome
_______________________________
|first_name|last_name|is_present|
---------------------------------
| D'Angelo |Williams | true |
---------------------------------
| Georgia | Fritz | false |
---------------------------------
| Luca | O'Keefe | false |
_______________________________

Javascript where clause for string that begins with text

Here is a sample of the Javascript, that I want to add an additional WHERE statement to the CONDITIONS variable.
In addition to Where Cost > 0, only include records where ProductName begins with "XYZ".
Maybe: AND ProductName StartsWith('XYZ') ??
REPORTS: [{NAME: 'MYREPORTNAME',
CONDITIONS: 'WHERE Cost > 0',
FIELDS: {'ProductSKU' : 'STRING',
'ProductName' : 'STRING',
'Cost' : 'FLOAT'
}
This is later called in this function:
function retrieveMyReport(reportConfig, ClientId) {
var fieldNames = Object.keys(reportConfig.FIELDS);
var report = MyApp.report(
'SELECT ' + fieldNames.join(',') +
' FROM ' + reportConfig.NAME + ' ' + reportConfig.CONDITIONS +
' DURING ' + CONFIG.DEFAULT_DATE_RANGE);
...
How do I expand this WHERE clause to include both conditions?
This looks like an SQL query, so you should probably use the LIKE operator:
CONDITIONS: 'WHERE Cost > 0 AND ProductName LIKE \'XYZ%\''

Array with repeating structure

I am creating a script that would show the keys of my array, in the first attempt worked perfectly, but when I added but a while block, he did not execute and returned this error:
classificacao-v2.js:128 Uncaught TypeError: Cannot read property 'Period' of undefined
at classificacao-v2.js:128
Realizing that my problem was in the variable 'n' that appeared as undefined, so I created other variables with different names for each structure.
I wonder if it is possible to rewrite it more efficiently without having to repeat each block.
let GoldemStates = [{Period: ' 1°',Points:'300'},
{Period: ' 2°',Points:'250'},
{Period: ' 3°', Points:'155'}]
let Chicago = [{Period: ' 1°',Points:'100'},
{Period: ' 2°',Points:'420'},
{Period: ' 3°', Points:'350'}]
let Broklyn = [{Period: ' 1°',Points:'300'},
{Period: ' 2°',Points:'250'},
{Period: ' 3°', Points:'155'}]
// Show the Teams
icons('','Match Results ','div_titulo')
let n = 0
icons('golden','Golden States', 'destaque_golden') //Team Title (Symbol, Team Name, CSS)
//Goldem States Statistics
do {
icons('clock',GoldemStates[n].Period + ' Period | ' + 'Points ' + GoldemStates[n].Points ,'texto') // // Show period and points
n ++
} while (n < GoldemStates.length);
let d = 0 // <-------- CHANGE WHICH WOULD NEED
//Chicago Bulls Statistics
icones('bulls','Chicago Bulls', 'destaque_bulls')//Team Title (Symbol, Team Name, CSS)
do {
icons('clock',Chicago[d].Period + ' Period | ' + 'Points ' + Chicago[d].Points ,'texto')// Show period and points
d ++
} while (d < Chicago.length);
Console Output
I think your code could be simpler, shorter and easier to read if you leave the iteration to array built in methods. That way you will remove the need to use an iteration variable and access each item:
GoldemStates.forEach(
item => icons('clock',item.Period + ' Period | ' + 'Points ' + item.Points ,'texto')
)
But we can do it eve better. Since all the teams render exactly the same, we can build a function that renders one single item and then let the methods specialized on iteration do their work. That way your code only takes care of rendering and the built-in methods takes care of iterating, separation of conerns:
const renderTeam = team => icons('clock',team.Period + ' Period | ' + 'Points ' + team.Points ,'texto')
// Render part
icons('golden','Golden States', 'destaque_golden') //Team Title (Symbol, Team Name, CSS)
GoldemStates.forEach(renderTeam)
icons('bulls','Chicago Bulls', 'destaque_bulls')//Team Title (Symbol, Team Name, CSS)
Chicago.forEach(renderTeam)

Javascript add all values with similar fileld name

I have a table as below
------------------------------------
| Date | Price |
------------------------------------
| 09-13-2017 | $15.00 |
| 09-13-2017 | $6.00 |
| 09-15-2017 | $8.00 |
| 09-15-2017 | $14.50 |
|__________________________________|
I want to sum the total values of each Date so the output would be
Total value for 09-13-2017 is $21.00
Total value for 09-15-2017 is $22.50
How do i do this in JavaScript. Kindly guide.
var dat = jQuery("#date").map(function()
{
totpri = 0;
var pri = jQuery("#price").map(function()
{
var totpri = document.getElementByID("price").value();
totpri += totpri;
}
alert('The Total is: ' totpri)
}).get();
I can give you a technique you can use but not the code ( since I don't see your effort )
Step 1: create an empty associative array (say arrDates)
Step 2: Iterate through each row and check if the array contains a key with the keyname as 1st column(date)
If NOT => create a new entry in the format: {'date' => amount}
If YES => get the old amount for that date, and add the current amount to it, update the amount in the array
That's it
Now you can iterate this array and display the dates and amount sum in whatever format you want

Categories