/    Sign up×
Community /Pin to ProfileBookmark

Template literal display control

Is there an acceptable way to handle missing fields in a template literal?
In my code example, the period after an initial appears when not desired and has extras when included.
How are initials handled (or suffixes for surnames) with variables in a template?

[code]
<!DOCTYPE html><html lang=”en”><head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width,initial-scale=1.0, user-scalable=yes”/>
<title> Test Page </title>
</head><body>
<pre id=’demo’></pre>

<script>
const write = (msg) => {
console.log(msg);
document.getElementById(‘demo’).append(msg+’n’);
}

const characters = [
{ “fname” : “Sylvester”, “MI” : “T”, “lname” : “Cat”, ‘joined’ : new Date(2020,1,1) },
{ “fname” : “Elmer”, “MI” : “”, “lname” : “Fudd”, ‘joined’ : new Date(2020,7,31) },
{ “fname” : “Tweety”, “MI” : “T.”, “lname” : “Bird”, ‘joined’ : new Date() },
{ “fname” : “Bugs”, “MI” : “B.”, “lname” : “Bunny, Esq.”, ‘joined’ : new Date() }
]

write(‘Problem below is the display of “.” when middle initial is blank’);
write(‘or double “..” when middle initial is not blank’);
write(‘n’);

for (const key of Object.keys(characters)) {
write(`${key} : ${characters[key].lname}, ${characters[key].fname} ${characters[key].MI}. tDate: ${characters[key].joined.toDateString()}`);
}
write(‘n’);

Object.entries(characters).forEach(
([key, value]) => write(`Date: ${value.joined.toDateString()} : ${value.fname} ${value.MI}. ${value.lname} t${key}`) )
</script>
</body></html>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@NachfolgerNov 13.2020 — Use a ternary statement.

Eg.
``JavaScript<i>
</i>${value.MI ? value.MI + "." : ""}<i>
</i>
`</CODE>
Or you can separate the display of the period and the initial
<CODE lang="JavaScript">
`JavaScript<i>
</i>${value.MI}${value.MI ? "." : ""}<i>
</i>
``


Plenty of other approaches.
Copy linkTweet thisAlerts:
@JMRKERauthorNov 14.2020 — Thank you. The 1st suggestion works better than the second for my purposes. :)

I'll work a bit more to eliminate the '..' display for some of the middle initials

or use some logic to remove the raw '.' before using the ternary for the correct display
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...