html52.com
我爱小程序-开发者交流社区

请问如何简化这种写法

问题模块 框架类型 问题类型 操作系统 工具版本
开发者工具 小程序 需求 Windows 1.02.1902010


//对于js中很有规律的如下片段:

    Txt={}

    if (Knowledge[TabID].content0 != undefined) { Txt.Content0 = "x" + Knowledge[TabID].content0 + "\n" } else { Txt.Content0 =""}

    if (Knowledge[TabID].content1 != undefined) { Txt.Content1 = "x" + Knowledge[TabID].content1 + "\n" } else { Txt.Content1 = "" }   

    if (Knowledge[TabID].content2 != undefined) { Txt.Content2 = "x" + Knowledge[TabID].content2 + "\n" } else { Txt.Content2 = "" }

    if (Knowledge[TabID].content3 != undefined) { Txt.Content3 = "x" + Knowledge[TabID].content3 + "\n" } else { Txt.Content3 = "" }

    if (Knowledge[TabID].content4 != undefined) { Txt.Content4 = "x" + Knowledge[TabID].content4 + "\n" } else { Txt.Content4 = "" }

    if (Knowledge[TabID].content5 != undefined) { Txt.Content5 = "x" + Knowledge[TabID].content5 + "\n" } else { Txt.Content5 = "" }



//  如何简化写法? 

网友回复

拾忆:

var Txt = {

content0: "",

content1: "",

content2: ""

}

for(var key in Txt){

Txt[key] = Knowledge[TabID][key] ? "x" + Knowledge[TabID].content0 + "\n" : ''

}


凤凰县铭锋计算机科技有限公司:

写function函数简化

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

真正的个人免签约支付接口

云免签H5支付