I get some data from the database using the following sql command:
SELECT users.skin, users.name, users.money, users.money_bank, users.fraction_id, users.fraction_id2, users.family_id, users.online_time, ban_list.ban_from, ban_list.ban_to, ban_list.count, ban_list.datetime, ban_list.reason FROM users LEFT OUTER JOIN ban_list ON users.name = ban_list.ban_to WHERE users.social = ? LIMIT 3
the response would look like this:
Now I create a Json stringified object to use it later on using the following code:
let charackters = [];
let nameData = row['name'].split(" ");
charackters.push({
spawnIndex: 0,
name: nameData[0],
surname: nameData[1],
level: 0,
cash: row['money'],
bank: row['money_bank'],
gender: gender,
fraction: fraction,
family: row['family_id'],
playedTime: row['online_time'],
ban: {
penalty: row['count'],
reason: row['reason'],
admin: row['ban_from'],
dateOfBan: row['datetime']
}
});
let charackterObject = {
characters: charackters
}
let charackterJson = JSON.stringify(charackterObject);
My problem is that the following part:
ban: {
penalty: row['count'],
reason: row['reason'],
admin: row['ban_from'],
dateOfBan: row['datetime']
}
should only be added if penalty, reason, admin, dateOfBan have values unlike NULL. How could i archive this since i can’t use a if statement?
To create a if statement
2
Answers
You can have an if
Here’s how you could do it as objects are pushed onto
charackters
: