Javascript 为casperjs中的对象指定子对象

Javascript 为casperjs中的对象指定子对象,javascript,object,casperjs,assign,Javascript,Object,Casperjs,Assign,我正在尝试分配给嵌套对象(在casperjs中): 这将在数组中推送相同的元素。但是有两个。我怎么能同时推两次,而不是两次 这不起作用: for (var i = 0; i < lengthch; i++){ alls.push(thisch[i].attr("value")); //Note [i] here. } for(变量i=0;i

我正在尝试分配给嵌套对象(在
casperjs
中):

这将在
数组中推送相同的元素。但是有两个。我怎么能同时推两次,而不是两次

这不起作用:

for (var i = 0; i < lengthch; i++){
    alls.push(thisch[i].attr("value")); //Note [i] here.
}
for(变量i=0;i
在黑暗中拍摄,因为上下文未知,但请尝试:

var yearz = [];
var yearz2 = $('#form_model optgroup');
yearz2.each(function(i, item) {
    var theyear = $(item).attr('label');        
    var theobj = {
        Year: theyear,
        Thing: [{}]
    };

    for (var i = 0; i < $(item).children().length; i++){
        theobj["Thing"].push({Name: $(item).children()[i].attr("value")});
    }

    yearz.push(theobj);
});
var yearz=[];
var yearz2=$('form#u model optgroup');
年份Z2.每个(职能(一,项目){
变量年份=$(项目).attr('label');
var theobj={
年份:年份,,
事物:[{}]
};
对于(变量i=0;i<$(项).children().length;i++){
推送({Name:$(item).children()[i].attr(“value”)});
}
yearz.push(theobj);
});
这不起作用,因为[]返回的是DOM,而不是jQuery对象,并且DOM没有.attr()

应该如此

thisch.eq(i).val()

也可以只使用each()或map()而不是for循环

var values = thisch.map( function() { return this.value; } ).get();

你需要解释一下“不起作用”的意思@Satpal不起作用。返回的对象是
null
返回的对象是什么?您在该代码中没有返回语句。@user1665355是控制台错误
uncaughttypeerror:$(…).children(…)[i]。attr不是函数(…)
?@AlexSzabóObject是
null
没有对象仍然是
null
在分配了yearz2.length之后,它会报告什么?在作业后添加一个提醒以进行检查?非常感谢!我不知道。所以
[]
返回DOM?
alls.push(thisch[i].attr("value")); //Note [i] here.
thisch.eq(i).val()
thisch[i].value
var values = thisch.map( function() { return this.value; } ).get();