Foreach循环在数组/对象内

时间:2016-01-26 17:16:15

标签: javascript

我对Javascript没有很好的理解,所以我不知道我是否正确地提出了我的问题。 如果我有一个包含对象的数组,如何用mydata值填充和替换这个'values'?我需要某种foreach循环,我不知道会有多少个对象。

function Person () {
}

Person.prototype.setName = function (n) {
  this.name = n;
}

Person.prototype.name = "no name"; // Define the name directly

function Student () {
  Person.call(this); // you only need this, if there are other things happening in the Person constructor that you need as well
  this.id = "123";
}
Student.prototype = Object.create(Person.prototype);

s = new Student();
// s.name is 'no name'
s.setName("Klaus");
// s.name is 'Klaus'

mydata看起来像这样

editor.addButton( 'gavickpro_tc_button', {
            fixedWidth: true,
            values: [
            {
                text: mydata.knjiga,
                value: mydata.shortcode,
                onclick: function() {
                    editor.insertContent(this.value());
                }
            },
            {
            text: mydata.knjiga2,
                value: mydata.shortcode2,
                onclick: function() {
                    editor.insertContent(this.value());
                }
            }
            {
                text: mydata.knjiga3,
                value: mydata.shortcode3,
                onclick: function() {
                    editor.insertContent(this.value());
                }
            }
            ]
});

1 个答案:

答案 0 :(得分:0)

原始JavaScript没有foreach循环:http://www.w3schools.com/js/js_loop_for.asp。要迭代数组中的每个元素,您可以使用for循环,如下所示:

for (i = 0; i < objectArray.length; i++) {
    object= objectArray[i];
    object.knjiga = "Best book";
}

在jQuery中,每个函数都是http://api.jquery.com/jquery.each/并实现foreach循环。

相关问题