javascript - 如何在 Typescript Push 方法中编写 If Else
问题描述
我有一个推送方法,我正在推送一些键值对。我想写一个if-else condition
。我想categories
使用 If-else 条件设置(在'key':row [0],'value':row [1]之后)的值,而不是将其硬编码为通用设置。可能吗。如果是,那怎么办?
Object.entries(this.generalInfoForm.value).forEach((row, index) => {
console.log(this.finalObject.properties);
// index < 5 ? this.finalObject.properties.push({ 'key': row[0], 'value': row[1], 'categories': objMap[row[0]] }) : '';
index < 5 ? this.finalObject.properties.push({
'key': row[0], 'value': row[1], 'categories': 'general-settings'
}) : '';
});
解决方案
不要想太多。只需categories
提前确定值并使用它:
Object.entries(this.generalInfoForm.value).forEach((row, index) => {
console.log(this.finalObject.properties);
if (index < 5) {
const categories =
condition
? trueValue
: falseValue;
this.finalObject.properties.push({
'key': row[0],
'value': row[1],
categories,
});
}
});
如果您真的想将它组合到对象创建表达式中,当然可以,但我不推荐它,因为它不利于可读性:
this.finalObject.properties.push({
'key': row[0],
'value': row[1],
'categories': condition ? trueValue : falseValue,
});
推荐阅读
- python - 在给定语料库的情况下构建 TFIDF Vectorizer 并使用 Sklearn 比较其结果?
- json - KeyCloak JSON 导入管理员
- firebase - 在向父集合添加数据时将子集合添加到父集合
- java - 如何迭代for循环并从List中获取值
- >>
- php - 您的 PHP 安装似乎缺少 wordpress for redhat 所需的 MYSQL 扩展
- python - 如何在多重线性回归中测试所有可能的迭代并返回最佳 R-Squared 和 P 值组合
- python - Tensorflow keras layers.Multiply() TypeError: 'NoneType' object is not subscriptable
- c++ - 如何根据滑块的位置正确确定变量的值
- autosys - Autosys - 释放作业依赖
- json - Bin 图标从 Visual Studio Code 的面板中消失