skip to Main Content

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:

Response from sql query

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


  1. You can have an if

    const obj = {
      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']
     }
     const ban = {};
     if (row['count']) ban["penalty"] = row['count'];
     if (row['reason']) ban["reason"] = row['reason'];
     if (row['ban_from']) ban["admin"] = row['ban_from'];
     if (row['datetime']) ban["dateOfBan"] = row['datetime'];
     if (Object.keys(ban).length > 0) obj["ban"] = ban;
     
     charackters.push(obj);
    
    Login or Signup to reply.
  2. Here’s how you could do it as objects are pushed onto charackters:

    // Create the ban object
    const ban = {
      penalty: row["count"],
      reason: row["reason"],
      admin: row["ban_from"],
      dateOfBan: row["datetime"],
    };
    
    // Only add the `ban` object as a property if all 
    // of the properties of the `ban` object have a value.
    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"],
      ...(Object.values(ban).every((value) => value) ? { ban } : {}),
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search