How to make to pass the values?

There is an ajax request, how can I pass all the values of the item.id new_id ? (passed only the last one)

var info_id = null;
var new_id = null;

$.ajax({
 url: "/api/myinv",
 type: 'GET',
 dataType: 'json',
 beforeSend: function(){
 $('#my_inventory').html('<div class="sk-sk spinner-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div><br></div><br>Loading message, please wait...');
},

 success: function(data) {
 var str = "";
 data.items.forEach(function(item, i, arr){
new_id= item.id; // the item.id stored a lot of values

 str += '<div class="mini_skin gray_gradient col-lg-1">' +
 '<div class="mini_info">' +
'<span class="stat_trak">' + 
'<input type="checkbox" data-item-id="' + item.id + '" data-item-class="'+ item.classid +'" data-item-price="'+ item.price +'" name="somename'+ item.id +'" class="itemsInv" value="1"></span>'+
'<span class="price shadow1" price>'+item.price+' <i class="fa fa-diamond"></i></span>'+
'</div>' +
'<img src="' + item.icon_url + '" alt="skin2" class="shadow1">' +
'<div class="name_box restricted_gradient">' +
'<span class="weapon_name shadow1">'+item.name+'</span><span class="weapon_type shadow1"></span></div></div>';

 }); 
$("#my_inventory").html(str);
 $("#my_inventory").append("<div style="clear:both"></div>"); 
}//

})
}
 else {console.log("Error");}
}
});
 return false
}
);

$(document).on('change', 'input.itemsInv', function(){

console.log("new_id =", new_id); // Get only the last value of item.id

});
July 8th 19 at 11:13
1 answer
July 8th 19 at 11:15
You iterate through the items one by one (forEach) instead of all at once. Therefore, in item.id is only one id value for this item. It turns out that in new_id is recorded .id of the last item.

What would have been new_id id of all items, set new_id as an array:
new_id = [];
And store each id in the array:
new_id.push(item.id)
(instead of new_id = item.id)

The output will get an array with all id new_id.
Is it possible to new_id.push to fit item.id and item.price ?
because you need to fulfill the following condition is an example.

// Search price id 6708286210
if (new_id.push(item.id) == '6708286210'){
console.log("new_price = ", new_price.push(item.price));
} - clemens66 commented on July 8th 19 at 11:18
Method .push adds an element to the array. Why are you trying to call it in console.log in if()? Will be examined to start with. - kiley_Gottli commented on July 8th 19 at 11:21

Find more questions by tags JavaScript