How to make to pass the values?

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

var info_id = null;
var new_id = null;

 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=; // the 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="' + + '" data-item-class="'+ item.classid +'" data-item-price="'+ item.price +'" name="somename'+ +'" 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">''</span><span class="weapon_type shadow1"></span></div></div>';

 $("#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

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 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:
(instead of new_id =

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

// Search price id 6708286210
if (new_id.push( == '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