Jquery数组中的最高最近值返回未定义的值
首先,我知道关于这个问题有很多话题。但我就是找不到它在我的情况下不起作用的原因 我试图找到数组中最近的最高值数量。此数组是动态创建的。 因此,在下面的示例中,当前值/数量为17。我想从数组中返回包含qty:60的对象 无论我尝试什么,输出都是未定义的。我就是不明白为什么会这样Jquery数组中的最高最近值返回未定义的值,jquery,arrays,json,Jquery,Arrays,Json,首先,我知道关于这个问题有很多话题。但我就是找不到它在我的情况下不起作用的原因 我试图找到数组中最近的最高值数量。此数组是动态创建的。 因此,在下面的示例中,当前值/数量为17。我想从数组中返回包含qty:60的对象 无论我尝试什么,输出都是未定义的。我就是不明白为什么会这样 <div class="product" data-url="some-url-to-json" data-quantity="17"> .... </div> $(function(){
<div class="product" data-url="some-url-to-json" data-quantity="17">
....
</div>
$(function(){
$('#form-pagecart .product').each(function(){
var url = $(this).data('url') + '?format=json';
var cur_qty = $(this).data('quantity')
var highestArray= []
$.getJSON(url, function(data){
$.each(data.product.discounts, function(index, disc) {
highestArray.push({ qty: parseInt(disc.quantity), price: disc.price.price_incl, price_excl: disc.price.price_excl })
});
});
/* things I tried ======> */
var output = highestArray.slice().reverse().find(({ qty }) => qty <= cur_qty);
var nearest = highestArray.sort(function(a, b){
return Math.abs(a.qty - cur_qty) - Math.abs(b.qty - cur_qty)
})[0];
console.log(output, nearest)
});
为什么保持console.log返回未定义的输出和最近的
我还尝试使用如下函数:
changePrice(highestArray, cur_qty)
function changePrice(array,num){
var output = array.slice().reverse().find(({ qty }) => qty <= num);
if(output){
$('.price .price-new i').text(output['price'])
$('.price .vat i').text(output['price_excl'])
}
}
在您尝试获取最高值时,服务器可能没有返回您需要的JSON 试着这样做:
$('#form-pagecart .product').each(function(){
var url = $(this).data('url') + '?format=json';
var cur_qty = $(this).data('quantity')
var highestArray= []
$.getJSON(url, function(data){
$.each(data.product.discounts, function(index, disc) {
highestArray.push({ qty: parseInt(disc.quantity), price: disc.price.price_incl, price_excl: disc.price.price_excl
});
// Do it inside the callback, so you can be sure that the
// JSON is already there.
var output = highestArray.slice().reverse().find(({ qty }) => qty <= cur_qty);
});
});
这回答了你的问题吗?您应该尝试的事情:console.loghighestArray在使用它之前,$.getJSON异步运行,因此$.each..push。。当highestArray不为空,但您声明您期望数量为60时,您的“我尝试的”返回数量为15的条目。不太清楚17和60的最高最近值是多少,这不是最高值吗?@freedomn-m:你是对的。我的意思是下一个值。因此,当当前值为17时,数组中的下一个值为60。所以我想返回60的对象。当当前值为例如10时,下一个值将为15。但是当我有15个值时,下一个应该返回的值是60,不用担心,这不是真正的问题,只是在看的时候注意到了。您的问题是$.getJSONHi的异步,这是一个很好的答案。然而,在这种情况下,这个问题或它的变体每天至少被问10次,而且有一个很好的答案详细解释了这个问题还有一个类似的答案也详细解释了,我总是使用上面的答案。我们不是每次都重复答案,而是提供一个链接,并以重复的方式投票。你很快就会有足够的代表投票。您可能会对此感兴趣:
$('#form-pagecart .product').each(function(){
var url = $(this).data('url') + '?format=json';
var cur_qty = $(this).data('quantity')
var highestArray= []
$.getJSON(url, function(data){
$.each(data.product.discounts, function(index, disc) {
highestArray.push({ qty: parseInt(disc.quantity), price: disc.price.price_incl, price_excl: disc.price.price_excl
});
// Do it inside the callback, so you can be sure that the
// JSON is already there.
var output = highestArray.slice().reverse().find(({ qty }) => qty <= cur_qty);
});
});