如何搜索对象以总计特定地址的所有结果?
$.ajax({
url: "https://snowgem.voidr.net/api/payments",
dataType: 'json',
success: function (data) {
var addr = 's1QR1uT4GvVWgct1sRU226rXiTzCPwf72J3';
// find object
var payments = data.find(function (obj) {
return obj.payments.amounts == addr;
});
console.log(payments);
},
error: function() {
//alert("Was unable to get info!");
}
});
答案 0 :(得分:1)
以下是如何使用js从amounts
获取给定addr
地址的总数 -
$.ajax({
url: "https://snowgem.voidr.net/api/payments",
dataType: 'json',
success: function(data) {
var addr = 's1QR1uT4GvVWgct1sRU226rXiTzCPwf72J3';
// find object
var totalAmount =
data[0].payments.
map(payment => payment.amounts). //get all amount object
map(amounts => amounts[addr]). //get amount from addr
filter(price => price). //filter undefined amount
reduce((first, second) => { //sum amount
return first + second;
});
console.log(totalAmount);
},
error: function() {
//alert("Was unable to get info!");
}
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
点击Run code snippet
验证输出。 :)