Javascript 如何在typescript中将这些对象转换为数组
如何将字符串转换为Typescript数组?。请帮帮我 看看代码:Javascript 如何在typescript中将这些对象转换为数组,javascript,arrays,angular,typescript,Javascript,Arrays,Angular,Typescript,如何将字符串转换为Typescript数组?。请帮帮我 看看代码: private validateEmptyOption(): any { console.log("CHECKED") let isValid = true; this.currentForm.sections.forEach((section: Section) => { section.fields.forEach((field: Field) => {
private validateEmptyOption(): any {
console.log("CHECKED")
let isValid = true;
this.currentForm.sections.forEach((section: Section) => {
section.fields.forEach((field: Field) => {
debugger
if (field.type === 'select') {
console.log(field)
const emptyOptionSections = JSON.parse(field.property).emptyOptionSections;
if ((emptyOptionSections !== undefined) && (emptyOptionSections == null)) {
alert('QAZWSX');
isValid = false;
}
}
return isValid;
});
});
}
这是我在控制台中的结果。我想把它转换成数组并循环
预期结果:应该有一条验证消息,通知select controller不能为空
实际结果:表单无需任何验证即可保存,因为您的
return
语句写在了错误的位置。
检查
{
}
(
)
如果使用validateEmptyOption():boolean
则会得到函数不返回任何值的提示。问题在于,如果在其他函数中,return语句没有被主函数返回
几乎从不需要将any用作带有typescript的类型。(直到你做了非常复杂的事情)
函数validateEmptyOption():布尔值{
控制台日志(“已检查”)
返回此.currentForm.sections.every((节:节)=>{
return section.fields.every((field:field)=>{
调试器
如果(field.type!=“选择”){
返回false;
}
console.log(字段)
试一试{
const emptyOptionSections=JSON.parse(field.property).emptyOptionSections;
if((emptyOptionSections!==未定义)&&(emptyOptionSections==空)){
警报(“QAZWSX”);
返回false;
}
}捕获(e){
返回false;
}
返回真值
});
});
}
用于检查内部函数是否为每个值返回true。您要转换为数组的字符串是什么?@Plochie我附加了它。您不能使用JSON解析该字符串。parse?好的,我怎么做。你能帮我吗@请编辑问题以包含这些更改,而不是将其作为评论发布。还有,这个“对象”应该是什么样子?环顾四周,通读这本书。特别地