简体   繁体   English

如何在实现SimpleSchema的Meteor中遍历包含对象的数组

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

I have a simple schema implementation as follows. 我有一个简单的架构实现,如下所示。 There is an array containing objects. 有一个包含对象的数组。 Each object has a radio button. 每个对象都有一个单选按钮。 I need to extract the value of each radio button. 我需要提取每个单选按钮的值。 How do I traverse through the array? 如何遍历数组?

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);


     }
}

I tried an incremental approapch where I removed the array definition and just to retrieve the data from objets alone. 我尝试了增量方法,其中删除了数组定义,仅从objets中检索数据。 Someethiing like this worked: 这样的工作奏效了:

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

However, after encompassing the object within an array, something like this does not work. 但是,在将对象包含在数组中之后,类似的操作将不起作用。

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

What works is : 有效的是:

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

This retrieves the first object's condition value. 这将检索第一个对象的条件值。 How do I, lets say extract other elements? 我如何说提取其他元素?

The '$' sign is a placeholder for the array index in the Schema definition. “ $”符号是模式定义中数组索引的占位符。

When accessing actual instances you replace it with an actual array index. 当访问实际实例时,可以将其替换为实际的数组索引。

So the first one has index 0, the next one has index = 1, and so on: 因此,第一个索引为0,下一个索引为1,依此类推:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM