如何在流星实现SimpleSchema中迭代包含对象的数组

How to iterate through an array containing objects in Meteor implementing SimpleSchema

本文关键字:包含 迭代 对象 数组 SimpleSchema 流星 实现      更新时间:2023-09-26

我有一个简单的模式实现,如下所示。有一个包含对象的数组。每个对象都有一个单选按钮。我需要提取每个单选按钮的值。如何遍历数组?

test_schema = new SimpleSchema ({

    object:{
        type:Array,
    },
    "object.$":{
        type:Object
    },
    "object.$.condition" :{
        type:String,
        autoform:{
            type: "select-radio-inline",
            options:[{label:'1', value:"one"}]
        },
    },

 "zod": {
    type: String,
     optional:true,
     custom: function () {
         alert(this.field('object').value);

     }
}

我尝试了一种增量方法,我删除了数组定义,只是从对象单独检索数据。像这样的东西起作用了:

this.field('object.condition').value

但是,在数组中包含对象之后,这样的操作就不起作用了。

this.field('object.$.condition').value

有效的方法是:

this.field('object.0.condition').value

检索第一个对象的条件值。我怎么提取其他元素呢?

'$'符号是Schema定义中数组索引的占位符。

访问实际实例时,用实际数组索引替换。

第一个索引为0,下一个索引为1,以此类推:

for (var i=0, i<object.length, i++) {
  console.log( i, this.field('object.'+i+'.condition').value
}