You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kendo UI中访问对象属性并修正现有代码问题

问题描述

我不熟悉Kendo UI,但正尝试让代码正常运行。需求为:访问对象的属性,检查属性名是否匹配指定正则模式/^\d+TipoParentela$/,再根据属性值为madreIdx、padreIdx等变量赋值。

以下是我编写的代码:

#var propertiesKeys = Object.keys(properties);#
#var madreIdx, padreIdx, coniugeIdx, vedovanzaIdx, exConiugeIdx, conviventeIdx;# 


#for (var i = 0; i < propertiesKeys.length; i++) {#
    #var propertyName = propertiesKeys[i];#
    #if (/^\d+TipoParentela$/.test(propertyName)) {#
        <!-- Assign values to variables based on conditions -->
        #if (properties[propertyName].value == "GenitoreAnpr") {#
            # var genitoreIdx = i.toString();#
            #if (properties[genitoreIdx + "Sesso"] && properties[genitoreIdx + "Sesso"] == "M") { padreIdx = i.toString(); }#
            #if (properties[genitoreIdx + "Sesso"] && properties[genitoreIdx + "Sesso"] == "F") { madreIdx = i.toString(); }#
        #} else if (properties[propertyName].value == "ConiugeAnpr") {#
            #coniugeIdx = i.toString();#
        #} else if (properties[propertyName].value == "VedovanzaAnpr") {#
            #vedovanzaIdx = i.toString();#
        #} else if (properties[propertyName].value == "ExConiugeAnpr") {#
            #exConiugeIdx = i.toString();#
        #} else if (properties[propertyName].value == "ConviventeAnpr") {#
            #conviventeIdx = i.toString();#
        #}#
    #}#
#}#

其中第一行代码var propertiesKeys = Object.keys(properties);是JavaScript写法,我怀疑无法在Kendo UI中正常运行,请问该如何修改代码以实现需求?

解决方案

Kendo UI模板的# #标记本质上支持标准JavaScript,但你的代码存在逻辑错误和可优化点,以下是修正后的实现:

核心问题修正

你之前用循环索引i作为Sesso属性的前缀是错误的——正确的前缀应该是匹配属性名中的数字部分(比如1TipoParentela对应1Sesso),而非数组循环的索引值。

修正后的代码

#var madreIdx, padreIdx, coniugeIdx, vedovanzaIdx, exConiugeIdx, conviventeIdx;# 

#// 遍历properties对象的所有属性#
#for (var propertyName in properties) {#
    #// 跳过原型链上的属性#
    #if (!properties.hasOwnProperty(propertyName)) continue;#
    
    #// 检查属性名是否匹配正则#
    #if (/^\d+TipoParentela$/.test(propertyName)) {#
        #var propValue = properties[propertyName].value;#
        #// 提取属性名中的数字前缀#
        #var idxPrefix = propertyName.match(/^\d+/)[0];#

        #if (propValue === "GenitoreAnpr") {#
            #var sessoProp = properties[idxPrefix + "Sesso"];#
            #if (sessoProp) {#
                #if (sessoProp.value === "M") { padreIdx = idxPrefix; }#
                #if (sessoProp.value === "F") { madreIdx = idxPrefix; }#
            #}#
        #} else if (propValue === "ConiugeAnpr") {#
            #coniugeIdx = idxPrefix;#
        #} else if (propValue === "VedovanzaAnpr") {#
            #vedovanzaIdx = idxPrefix;#
        #} else if (propValue === "ExConiugeAnpr") {#
            #exConiugeIdx = idxPrefix;#
        #} else if (propValue === "ConviventeAnpr") {#
            #conviventeIdx = idxPrefix;#
        #}#
    #}#
#}#

关键修改说明

  1. 改用for...in遍历:直接遍历对象属性,避免Object.keys()的兼容性顾虑(实际现代环境中Object.keys()完全可用,但for...in更贴合对象遍历场景),同时增加hasOwnProperty检查避免遍历原型链属性。
  2. 提取正确的数字前缀:用正则/^\d+/从匹配的属性名中提取数字部分,这才是对应Sesso属性的正确关联标识。
  3. 修正属性取值逻辑:统一使用.value获取属性值,和TipoParentela的取值方式保持一致,避免隐式类型错误。
  4. 使用严格相等判断:用===替代==,避免隐式类型转换导致的意外判断结果。

内容的提问来源于stack exchange,提问作者fabianod

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 21:55:26