当前返回所有结果,但是当未找到_miner时,它将在控制台中返回undefined并继续显示时间和块。没有返回_miner时如何不输出?
输出应该是这样的,其中时间“ 3”没有_miner匹配,因此没有任何输出。
1
123
10
2
124
10
4
123
10
$.ajax({
url: "https://xxxxxxxxxxxx/api/payments",
dataType: 'json',
success: function(data) {
var _miner = "t1XHpNtYY2N3EMDRoX9RM2hq4DWWPZSmawJ";
var addr = _miner;
for (var i = 0; i < data.length; i++) {
//Name
console.log(data[i].name);
var payments = data[i].payments;
for (var j = 0; j < payments.length; j++) {
//Time
console.log(payments[j].time);
//blocks
var blocks = payments[j].blocks;
for (var b = 0; b < blocks.length; b++) {
console.log(blocks[b])
}
console.log(payments[j].amounts[_miner]);
}
}
},
error: function() {
//alert("Was unable to get info!");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
答案 0 :(得分:0)
只需检查是否存在:
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
或更妙的是:
if (payments[j].amounts[_miner] === undefined)
console.log(payments[j].amounts[_miner]);
甚至:
if (payments[j].amounts[_miner] === undefined)
console.log(payments[j].amounts[_miner])
else
console.log("no payments")