@Alex10214

Как упростить такую запись?

Приветствую, может кто поможет упростить такую запись:
if (this.newAccount.model_settings !== "{}") {
      this.objForPost = `-n ${this.newAccount.account_name} -d ${this.newAccount.start_deposit} -c ${this.newAccount.client_name} -b ${this.newAccount.ib_account_number} -l ${this.newAccount.leverage} -e ${this.newAccount.description} -r ${this.newAccount.rebalance_model} -m "${this.newAccount.model_settings}"`;
    } else if (this.newAccount.target_weights !== "{}") {
      this.objForPost = `-n ${this.newAccount.account_name} -d ${this.newAccount.start_deposit} -c ${this.newAccount.client_name} -b ${this.newAccount.ib_account_number} -l ${this.newAccount.leverage} -e ${this.newAccount.description} -r ${this.newAccount.rebalance_model} -s ${this.newAccount.selected_systems} -w "${this.newAccount.target_weights}"`;
    } else if (this.newAccount.model_settings !== "{}" && this.newAccount.target_weights !== "{}") {
      this.objForPost = `-n ${this.newAccount.account_name} -d ${this.newAccount.start_deposit} -c ${this.newAccount.client_name} -b ${this.newAccount.ib_account_number} -l ${this.newAccount.leverage} -e ${this.newAccount.description} -r ${this.newAccount.rebalance_model} -s ${this.newAccount.selected_systems} -w "${this.newAccount.target_weights}" -m "${this.newAccount.model_settings}"`;
    }

Я только учусь, но мне кажется что выглядит не очень.
  • Вопрос задан
  • 101 просмотр
Решения вопроса 2
sergiks
@sergiks Куратор тега JavaScript
♬♬
const map = {
  n: "account_name",
  d: "start_deposit",
  c: "client_name",
  b: "ib_account_number",
  l: "leverage",
  e: "description",
  r: "rebalance_model",
  m: "model_settings",
  s: "selected_systems",
  w: "target_weights",
}

let key = "ndcbler";

const hasSettings = this.newAccount.model_settings !== "{}";
const hasWeights = this.newAccount.target_weights !== "{}";

if (hasSettings && hasWeights) {
  key += "swm";
} else if (hasSettings) {
  key += "m";
} else if (hasWeights) {
  key += "sw";
// } else { // что делаем, если ни того ни того нет?
//   key = "";
}

this.objForPost = key.split("")
  .map((key) => `-${key} ${this.newAccount[map[key]]}`)
  .join(" ");
Ответ написан
delphinpro
@delphinpro Куратор тега JavaScript
frontend developer
const с = `-n ${this.newAccount.account_name}`
       + ` -d ${this.newAccount.start_deposit}`
       + ` -c ${this.newAccount.client_name}`
       + ` -b ${this.newAccount.ib_account_number}`
       + ` -l ${this.newAccount.leverage}`
       + ` -e ${this.newAccount.description}`
       + ` -r ${this.newAccount.rebalance_model}`;

if (this.newAccount.model_settings !== "{}") {
  this.objForPost = с
      + ` -m "${this.newAccount.model_settings}"`;
} else if (this.newAccount.target_weights !== "{}") {
  this.objForPost = c
      + ` -s ${this.newAccount.selected_systems}`
      + ` -w "${this.newAccount.target_weights}"`;
} else if (this.newAccount.model_settings !== "{}"
        && this.newAccount.target_weights !== "{}"
) {
  this.objForPost = с
      + ` -s ${this.newAccount.selected_systems}`
      + ` -w "${this.newAccount.target_weights}"`
      + ` -m "${this.newAccount.model_settings}"`;
}


let {
  account_name,
  start_deposit,
  client_name,
  ib_account_number,
  leverage,
  description,
  rebalance_model,
  model_settings,
  selected_systems,
  target_weights,
} = this.newAccount;

const с = `-n ${account_name}`
       + ` -d ${start_deposit}`
       + ` -c ${client_name}`
       + ` -b ${ib_account_number}`
       + ` -l ${leverage}`
       + ` -e ${description}`
       + ` -r ${rebalance_model}`;

if (model_settings !== "{}") {
  this.objForPost = с + ` -m "${model_settings}"`;
} else if (target_weights !== "{}") {
  this.objForPost = c + ` -s ${selected_systems} -w "${target_weights}"`;
} else if (model_settings !== "{}" && target_weights !== "{}") {
  this.objForPost = с + ` -s ${selected_systems} -w "${target_weights}" -m "${model_settings}"`;
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы