2016-12-08 1 views
-2

casperjs에 반대합니다 :할당 어린이 (<code>casperjs</code>에서) 내가 중첩 된 객체에 할당하려고

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

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

     yearz.push(theobj); 
    }); 

this.echo(yearz) 반환 null 을 문제를 볼 수 있을까요?

안부

EDIT

var yearz = []; 
var yearz2 = $('#form_model optgroup'); 
yearz2.each(function() { 
    var theyear = $(this).attr('label'); 
    var lengthch = $(this).children().length; 
    var thisch = $(this).children();  
    var theobj = []; 
    var alls = []; 

    for (var i = 0; i < lengthch; i++){ 
     alls.push(thisch.attr("value")); 
    } 

    theobj = { 
     Year: theyear.trim(), 
     Thing: alls 
    }; 

    yearz.push(theobj); 
}); 

alls 배열 동일한 요소를 푸시

. 그러나 두 가지가 있습니다. 어떻게 둘 다 밀어 붙일 수 있습니까? 컨텍스트 알 수 없기 때문에,

for (var i = 0; i < lengthch; i++){ 
    alls.push(thisch[i].attr("value")); //Note [i] here. 
} 
+0

"작동하지 않음"이란 의미를 설명해야합니다. – Itay

+0

@Satpal이 작동하지 않습니다. 반환 된 객체는 'null'입니다. – user1665355

+0

객체를 반환했습니다. 해당 코드에는 return 문이 없습니다. – Quentin

답변

1
alls.push(thisch[i].attr("value")); //Note [i] here. 

는 DOM을 반환 , jQuery 객체가 아니며 DOM에는 .attr()이 없습니다.

thisch.eq(i).val() 

또는

thisch[i].value 

또한 단지 루프 대신의) 각() 또는지도를 (사용할 수 있습니다.

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

정말 고마워요! 그걸 몰랐어. 그래서'[]'는 DOM을 반환합니까? – user1665355

-2

어둠 속에서 촬영을하지만, 시도 :

이 작동하지 않습니다 [] 때문에 작동하지 않습니다

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); 
}); 
+0

'null'인 객체가 없습니다. – user1665355

+0

yearz2.length 보고서를 할당 한 후 그 보고서는 무엇입니까? 과제를 확인한 후 알림을 추가 하시겠습니까? – codemonkey65

+0

내 편집을 참조하십시오 – user1665355

관련 문제