gpt4 book ai didi

javascript - 使用 Meteor 中的事件从数组中检索元素

转载 作者:行者123 更新时间:2023-12-03 03:07:47 26 4
gpt4 key购买 nike

我正在“类别”函数中获取 [object, object] 而不是数组中的元素。虽然我不确定,但这也许是预料之中的,而我只是弄错了。

if (Meteor.isClient) {

Template.ipsosboard.helpers({
'categories': function() {
return array; // Some data stored as JS Object in lib.
},
'currentElement': function() {
return Session.get('selectedEvent');
}
});

Template.ipsosboard.events({
"change #category-select": function(event, template) {
var selectedEvent = $(event.currentTarget).val();
Session.set('selectedEvent', selectedEvent);
console.log("EventNum: " + selectedEvent);
}
});

}; //end of client code.

if (Meteor.isServer) {
//code to run by server here.
};

最佳答案

这似乎解决了这个问题。它需要将对象转换为数组。注意:“data”是一个json文件,作为JS对象存储在项目的lib文件夹中,因此我需要对其进行转换。

if (Meteor.isClient) {

Template.ipsosboard.helpers({
'categories': function() {

It needed to be converted to array using the function below.

        var myObj = data;
var array = $.map(myObj, function(value, index) {
return value;
});
return array;
},

So now it returns the data as required.

        'currentElement': function() {
return Session.get('selectedEvent');
}
});

Template.ipsosboard.events({
"change #category-select": function(event, template) {
var selectedEvent = $(event.currentTarget).val();
Session.set('selectedEvent', selectedEvent);
console.log("EventNum: " + selectedEvent);
}
});

}; //end of client code.

if (Meteor.isServer) {
//code to run by server here.
};

关于javascript - 使用 Meteor 中的事件从数组中检索元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47092377/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com