'For Loop inside Template Literals?
Is it a way to loop inside Template Literals? Obviously this could be done by mapping an array like this:
array = ["a", "b", "c"]
console.log(`foo ${array.map(i => i).join(" ")} bar`)
///foo a b c bar
But what if we need to loop somthing for specific times? like this:
`foo ${for (let i = 0; i <= 10; i++) {Somthing}} bar`
Solution 1:[1]
You can use reducer to achieve in ES6
const anArray = [
{
"name": "pulha",
"as": "puli"
},
{
"name": "puli",
"as": "moka"
},
{
"name": "moka",
"as": "starbucks"
},
{
"name": "starbucks",
"as": "sweet"
},
{
"name": "sweet",
"as": "krispey"
},
{
"name": "krispey",
"as": "free"
}
];
$('#an-example-showing-template').append(`
${anArray.reduce((updated, latest) => updated.concat(`<li>${latest.name} alias ${latest.as}</li>`), '')}
`)
#an-example-showing-template > li {
list-style-type: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="an-example-showing-template"></ul>
Solution 2:[2]
It would be better to implement your function outside the backtick expression like this:
function helloworld() {
let string;
for(let i = 0; i <10 ; i++){
string = 'Hello World!'
}
return string
}
//Inside the backtick
`${helloworld()}`
Solution 3:[3]
I guess you can try this to override spring-boot bom.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.16.0</version>
<scope>import</scope>
<type>pom</type>
</dependency>
... other dependencies including spring-boot-dependencies
</dependencies>
</dependencyManagement>
Solution 4:[4]
NOT GOOD/no effect(maybe for log4j1):
<org.apache.logging.log4j.version>2.16.0</org.apache.logging.log4j.version>
Better
(for spring-boot-parent-based [2-2.6.1]/spring-dependeny-managed):
<log4j2.version>2.16.0</log4j2.version>
;)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Amarnath Reddy Dornala |
| Solution 2 | ImLuctor |
| Solution 3 | Sukhmeet Sethi |
| Solution 4 | xerx593 |
