节点.JS无法从数组中选择值



我目前正试图从pricePerUnit

,但由于某些原因,它似乎找不到它,错误:Cannot read property 'pricePerUnit' of undefined

这是newJson.products[i].product_id/sell_summary[0]/buy_summary[0]的布局

布局:

'ENCHANTED_RAW_SALMON',
{ amount: 4101, pricePerUnit: 1571, orders: 1 },
{ amount: 32, pricePerUnit: 1601.4, orders: 1 },
'ENCHANTED_GLISTERING_MELON',
{ amount: 160, pricePerUnit: 12001.4, orders: 1 },
{ amount: 8, pricePerUnit: 15578.5, orders: 2 },
'PRISMARINE_SHARD',
{ amount: 4007, pricePerUnit: 1.5, orders: 1 },
{ amount: 10500, pricePerUnit: 3.8, orders: 3 },
'ENCHANTED_EMERALD',
{ amount: 64, pricePerUnit: 972, orders: 1 },
{ amount: 1195, pricePerUnit: 1013.5, orders: 1 },

代码:

for (i in newJson.products) {
var productName = newJson.products[i].product_id
var sellSummary = newJson.products[i].sell_summary[0].pricePerUnit
var buySummary  = newJson.products[i].buy_summary[0].pricePerUnit
var mathSummary = buySummary-sellSummary
array.push(
productName,
sellSummary,
buySummary
);

}
console.log(array);

我不太确定我做错了什么,但我希望你能帮助我:)。

//编辑

我收到了一个json请求它是这样的

{
"success": true,
"lastUpdated": 1632242967119,
"products": {
"BROWN_MUSHROOM": {
"product_id": "BROWN_MUSHROOM",
"sell_summary": [
{
"amount": 7857,
"pricePerUnit": 11.4,
"orders": 1
},
{
"amount": 14903,
"pricePerUnit": 11.3,
"orders": 1
},
{
"amount": 59218,
"pricePerUnit": 11.1,
"orders": 1
}
],
"buy_summary": [
{
"amount": 15704,
"pricePerUnit": 13.0,
"orders": 1
},
{
"amount": 664,
"pricePerUnit": 14.5,
"orders": 1
},
{
"amount": 1024,
"pricePerUnit": 14.6,
"orders": 1
}
],
"quick_status": {
"productId": "BROWN_MUSHROOM",
"sellPrice": 11.334521089630933,
"sellVolume": 1131729,
"sellMovingWeek": 10721858,
"sellOrders": 42,
"buyPrice": 14.346240421455938,
"buyVolume": 1698662,
"buyMovingWeek": 4432754,
"buyOrders": 785
}
},

这是完整的代码:

const axios = require("axios").default;
async function bazaar() {
try {
const res = await axios.get(
"https://api.hypixel.net/skyblock/bazaar?key=xxxxxxx"
);
var newJson = JSON.parse(JSON.stringify(res.data));
var array = [];
for (i in newJson.products) {
var productName = newJson.products[i].product_id
var sellSummary = newJson.products[i].sell_summary[0].pricePerUnit
var buySummary  = newJson.products[i].buy_summary[0].pricePerUnit
var mathSummary = buySummary-sellSummary
array.push(
productName,
sellSummary,
buySummary
);

}
console.log(newJson);
} catch (error) {
return console.log(error);
}
}

我想做的是:获取product_id,sell_summary[0].pricePerUnitbuy_summary[0].pricePerUnit

当我尝试访问product_id,sell_summary[0],buy_summary[0]

我得到了这样的东西:

'ENCHANTED_RAW_SALMON',
{ amount: 52447, pricePerUnit: 1570.6, orders: 1 },
{ amount: 493, pricePerUnit: 1600.9, orders: 1 },
'ENCHANTED_GLISTERING_MELON',
{ amount: 112, pricePerUnit: 12001.4, orders: 1 },
{ amount: 1, pricePerUnit: 15578.1, orders: 1 },
'PRISMARINE_SHARD',
{ amount: 65682, pricePerUnit: 1.5, orders: 1 },
{ amount: 3212, pricePerUnit: 3.8, orders: 1 },
'ENCHANTED_EMERALD',
{ amount: 38617, pricePerUnit: 971.8, orders: 2 },
{ amount: 66, pricePerUnit: 1012.1, orders: 1 },
'PROTECTOR_FRAGMENT',
{ amount: 9, pricePerUnit: 2400.3, orders: 1 },
{ amount: 4, pricePerUnit: 2793.2, orders: 1 },

这就是为什么我尝试访问pricePerUnit在每个销售&buy_summary

下面是一段代码片段,希望能帮助您找到问题所在。try-catch块被移到for循环中,这样一个产品发生的错误就不会中断循环。

更改了var newJson = JSON.parse(JSON.stringify(res.data));' tovar newJson = res.data;',因为我看不到克隆对象的目的。

async function bazaar() {
const res = await axios.get(
"https://api.hypixel.net/skyblock/bazaar?key=xxxxxxx"
);
var newJson = res.data;
var array = [];
for (i in newJson.products) {
try {
var productName = newJson.products[i].product_id
var sellSummary = newJson.products[i].sell_summary[0].pricePerUnit
var buySummary  = newJson.products[i].buy_summary[0].pricePerUnit
var mathSummary = buySummary-sellSummary
array.push(
productName,
sellSummary,
buySummary
);
} catch (error) {
console.log('problem reading product '+i, error.message)
}

}
console.dir(array)
}
bazaar()
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.4/axios.min.js"></script>

最新更新