2017-01-27 2 views
0

저는 모든 항목에 대해 JSONP 배열에서 네 개의 속성을 새로운 배열로 전송하려고하는 초보 프로그래머입니다.
1가 어떻게의 수에 따라 개체를 추가 할

bigarray = [{title:"xbox", url:"www.web.com", pic:"www.pic.com/w.png",price:"100"}, {title:"ps4", url:"www.web.com", pic:"www.pic.com/p.png",price:"110"}] 

질문 :

$.ajax({ 
    url: etsyURL, 
    dataType: 'jsonp', 
    success: function(data) { 
     if (data.ok) { 
      var a = (data.results); 
      //create a parent array to store all objects 
      var bigarray = []; 

      $.each(a, function(i, item) { 
       //assign values from response to variables 
       var atitle = item.title; 
       var aurl = item.url; 
       var aimg = item.Images[0].url_75x75; 
       var aprice = item.price; 

       //create an object 
       var object = { 
        title: atitle, 
        url: aurl, 
        img: aimg, 
        price: aprice 
       }; 

       //add the object into big array for every each item, unsure            
      }) 
     } 
    } 
}); 

내 최종 목표는 다음과 같은 객체의 모든 항목을 모두 얻을 수 bigarray를 얻는 것입니다 배열의 항목?
2. 다른 방법도 환영합니다. $ .even이 for 루프로 바뀌어도 답을 받아 들일 것입니다.

+0

다음과 같이 코드를 수정할 수 있습니다'[] = 객체를 bigarray,' – pixelarbeit

+0

경우'data.results을 ''$ .each()'-> ['Array.prototype.map()'] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/)가 필요없는 배열입니다./global_Objects/Array/map), 그렇지 않으면 ['$ .map()'] (https://api.jquery.com/jquery.map/) (+ ['.get()'] (https : /api.jquery.com/get/)) – Andreas

+0

푸시 기능을 사용하십시오. – Dimitri

답변

1

당신은 배열에 항목을 추가 푸시()을 사용할 수 있으며 당신도

$.ajax({ 
    url: etsyURL, 
    dataType: 'jsonp', 
    success: function(data) { 
     if (data.ok) { 
      var a = (data.results); 
      //create a parent array to store all objects 
      var bigarray = []; 

      $.each(a, function(i, item) { 
       //add the object into big array for every each item, unsure 
       bigarray.push({ 
        title: item.title, 
        url: item.url, 
        img: item.Images[0].url_75x75, 
        price: item.price 
       }); 

      }) 
     } 
    } 
}); 
+0

이것은 정확히 제가 원했던 것입니다, 많이 고마워요! –

1

배열의 끝에 값을 추가하는 데 사용할 수있는 push 메서드를 찾고 있습니다.

bigArray = []; // create the array 
object = {foo: 'bar'}; // create an object 
bigArray.push({object}); // push the object onto the end of the array 
관련 문제