简体   繁体   中英

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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