我有一个函数,它根据传入的特定规则检查顺序流中的当前阶段,并根据该值在我的Angular 2应用程序中分配下一个值.它看起来像这样:
private getNextStageStep(currentDisciplineSelected) {
const nextStageStep = '';
if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 1') {
const nextStageStep = 'step 2';
} else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 2') {
const nextStageStep = 'step 3';
} else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 3') {
const nextStageStep = 'step 4';
} else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 4') {
const nextStageStep = 'step 5';
} else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 5') {
const nextStageStep = 'step 6';
}
return nextStageStep;
}
Run Code Online (Sandbox Code Playgroud)
我在这里做的是返回"nextStageStep"的值,因为这就是我将要传递的内容,以便正确的阶段步骤发生.
现在,我的tslint使用警告"no shadowed variables"强调每个"nextStageStep"变量的出现.如果我删除我初始化为一个警告消失的空字符串的行,但是我得到错误,"找不到nextStageStep"出现在我的return语句中. …
我有一种情况需要比较和查找两个数组的共同值。我很清楚如何做到这一点,但不确定在这种情况下如何做到这一点。
我的第一个数组如下所示:
[ { kind: 'E',
path: [ 'short_name' ],
lhs: 'testing',
rhs: 'testing1' },
{ kind: 'E',
path: [ 'agent_name' ],
lhs: 'testing',
rhs: 'testing2' } ]
Run Code Online (Sandbox Code Playgroud)
上面的数组代表与文档更改有关的信息。
我的第二个数组如下所示:
[ { lhs: 'legacyId', rhs: 'id_number' },
{ lhs: 'name.short', rhs: 'short_name' },
{ lhs: 'name.long', rhs: 'agent_name' },
{ lhs: 'gender', rhs: 'gender' },
{ lhs: 'dob', rhs: 'date_of_birth' } ]
Run Code Online (Sandbox Code Playgroud)
我需要做的是遍历并在第一个数组的元素和第二个数组的“ rhs”值中找到“ path”的通用值。
因此,根据此处的示例,我应该最终找到以下值:short_name和agent_name。
我如何编写一个循环在两个数组上执行此操作?
我遇到了一个我试图理解的问题。我只是想通过使用 toDateString() 将日期转换为对读者更友好的格式。但是,这样做时我收到“toDateString() 不是函数”错误。
我可以使用 toString() 做到这一点:
truncateDate() {
if (this.employee && this.employee.dob)
{
let birthDate = this.employee.dob;
console.log(birthDate); // 2011-06-12T05:00:00.000Z Prints to console
console.log(birthDate.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
但我不能做 toDateString():
truncateDate() {
if (this.employee && this.employee.dob)
{
let birthDate = this.employee.dob;
console.log(birthDate); // 2011-06-12T05:00:00.000Z Prints to console
console.log(birthDate.toDateString());
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么?
有了Array.prototype.includes你可以做这样的事情:
let array1 = [1, 2, 3];
console.log(array1.includes(2)); // return true
Run Code Online (Sandbox Code Playgroud)
我的问题是:你可以使用includes一个对象数组,你想要找到的地方,比如"name"="Jane"?以下面的数据为例:
let array2 = [{"name" : "John", age: 24}, {"name" : "Jane", age: 36}]
这是你可以用这个includes方法做的事情- 它会是什么样子?
在更新我和同事正在开发的 Angular 应用程序的过程中,当我打算运行“npm install”时,我最终运行了“npm update”。这样做让我有点走投无路,因为现在我所有的依赖项 - 而且他们的依赖项在这个过程中得到了更新。从那里我必须解决某些冲突才能使新版本正常工作。但是,这也导致我发现这些依赖项之一中的错误阻止了我的应用程序启动。根据 Angular github repo,这个问题正在解决中。
我的问题是,在此期间我怎样才能恢复到以前的设置?我尝试复制并粘贴 package.json 文件,因为它在我的“npm 更新”之前最初存在,删除我的“节点模块”文件夹,然后再次运行“npm install”。但这并不能解决问题。有没有办法可以确保恢复到以前的工作设置?
在将数组转换为对象时,我知道诸如spread运算符和Object.assign()之类的选项,但是,我很难弄清楚如何将最终对象格式化为所需的格式。
我的原始数组如下所示:
let propsArray = [ { id: '1' },
{ 'name.first': 'john' },
{ 'name.last': 'smith' } ]
Run Code Online (Sandbox Code Playgroud)
我要从此数据中获取的对象应如下所示:
{
"id" : 1,
"name" : {
"first" : "john",
"last" : "smith"
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我尝试使用object.assign()进行的操作最终在结果对象中添加了数字作为属性键,这显然不是我想要的:
let finalObj = Object.assign({}, propsArray);
Run Code Online (Sandbox Code Playgroud)
我如何才能将生成的对象格式化为所需的格式?
我想用来map创建一个新的对象数组,该数组具有原始数组中原始对象的两个属性,所以我正在尝试这样做:
const filteredConfigurableModules = configurableModules.map(module =>
module.name, module.configurables
);
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为只有“名称”被保存到新数组中。我在这里缺少什么?
我也尝试过这个:
const filteredConfigurableModules = configurableModules.map(module => {
name: module.name,
configurables: module.configurables
});
Run Code Online (Sandbox Code Playgroud)
...但最终出现语法错误:
SyntaxError: Unexpected token ':'
我有一个for of循环,如果没有结果值,我需要退出。我突然想到我可以使用早期的return语句来处理这个问题,或者使用一个break语句。需要明确的是,在这种情况下,在我要跳过的部分之后的该代码块中没有其他代码可以执行,因此我假设其中任何一个都可以在这里工作(中断或返回)。在这种特殊情况下,有什么功能或性能原因需要使用其中一种而不是另一种吗?
选项 1:(中断)
for (let diff of differences) {
if (!diff.path) break;
if (diff.path[0] !== "updatedAt") {
const docChange = new ChangedProp(doc, diff, lastEditedBy, "customer");
docChange.log();
}
}
Run Code Online (Sandbox Code Playgroud)
选项 2:(返回)
for (let diff of differences) {
if (!diff.path) return;
if (diff.path[0] !== "updatedAt") {
const docChange = new ChangedProp(doc, diff, lastEditedBy, "customer");
docChange.log();
}
}
Run Code Online (Sandbox Code Playgroud) javascript ×8
arrays ×4
angular ×2
typescript ×2
break ×1
datetime ×1
for-of-loop ×1
npm ×1
npm-install ×1
npm-update ×1
object ×1
tslint ×1