'reducing the If else condtion code in javascript
I wrote a code to fetch the data present and store it in Array format but I thing I have wrote code multiple times can It be possible to minimize the code as its too long
let topicsValue = ["requiredType.*", "Entry.*", "token.*", "RestAPI.*"];
let Topic = [],
rest = ["required", "unrequired"],
final = ["createInput", "mustPossible", "finalOutput"];
topicsValue.map((data) => {
let requiredType, entries, token, restAPI;
if (data.split(".")[1].includes("*")) {
if (data.split(".")[0].includes("requiredType")) {
for (const value of final) {
requiredType = data
.split(".")[0]
.replace("requiredType", "required_type")
.concat(`.${value}`);
Topic.push(requiredType);
}
}
if (data.split(".")[0].includes("Entry")) {
for (const value of final) {
entries = data
.split(".")[0]
.replace("Entry", "entries")
.concat(`.${value}`);
Topic.push(entries);
}
for (const value of rest) {
entries = data
.split(".")[0]
.replace("Entry", "entries")
.concat(`.${value}`);
Topic.push(entries);
}
}
if (data.split(".")[0].includes("token")) {
for (const value of final) {
token = data
.split(".")[0]
.replace("token", "tokens")
.concat(`.${value}`);
Topic.push(token);
}
for (const value of rest) {
token = data
.split(".")[0]
.replace("token", "tokens")
.concat(`.${value}`);
Topic.push(token);
}
}
if (
data.split(".")[0].includes("RestAPI") &&
!data.split(".")[0].includes("RestAPIAction")
) {
restAPI = data
.split(".")[0]
.replace("RestAPI", "restAPI")
.concat(`.deploy`);
Topic.push(restAPI);
}
} else {
if (data.split(".")[0].includes("requiredType")) {
if (!rest.includes(data.split(".")[1])) {
requiredType = data
.split(".")[0]
.replace("requiredType", "required_type")
.concat(`.${data.split(".")[1]}`);
Topic.push(requiredType);
}
}
if (data.split(".")[0].includes("Entry")) {
if (rest.includes(data.split(".")[1])) {
entries = data
.split(".")[0]
.replace("Entry", "entries")
.concat(`.${data.split(".")[1]}.demo`);
Topic.push(entries);
} else {
entries = data
.split(".")[0]
.replace("Entry", "entries")
.concat(`.${data.split(".")[1]}.demo`);
Topic.push(entries);
}
}
if (data.split(".")[0].includes("token")) {
if (rest.includes(data.split(".")[1])) {
token = data
.split(".")[0]
.replace("token", "tokens")
.concat(`.${data.split(".")[1]}`);
Topic.push(token);
} else {
token = data
.split(".")[0]
.replace("token", "tokens")
.concat(`.${data.split(".")[1]}`);
Topic.push(token);
}
}
if (
data.split(".")[0].includes("RestAPI") &&
!data.split(".")[0].includes("RestAPIAction")
) {
restAPI = data
.split(".")[0]
.replace("RestAPI", "restAPI")
.concat(`.deploy`);
Topic.push(restAPI);
}
}
});
console.log(Topic);
Is there any possible way I can reduce the code without effecting the output
As the requirement of the code is like if the topicValue contain * or the other value so I wrote this long code and now I am trying to minimize the code so its look short and effective.
Solution 1:[1]
Try using the && operator.
if(y==1 && x == 1) {
do somthing
}
it only runs if both statements are true.
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 | Hermanboxcar |
