【发布时间】:2023-03-11 09:00:01
【问题描述】:
我有一个对象,我想更新 'marketBuyPrice' 和 'marketSellPrice' 条目以使用来自 fetch 调用的数据,如下所示:
//The object
const ores = {
Hemorphite :{ name: 'Hemorphite', marketRef: '1231', marketBuyPrice: '', marketSellPrice: ''},
Veldspar :{ name: 'Veldspar', marketRef: '1230', marketBuyPrice: '', marketSellPrice: ''},
};
//The fetch call
function getMarketPrices(oreType, id) {
var marketUrl = 'https://api.evemarketer.com/ec/marketstat?typeid='+id+'&usesystem=30000142';
fetch(marketUrl)
.then(response => response.text())
.then(str => (new DOMParser()).parseFromString(str, "text/xml"))
.then(data => {
var buyPrice = data.getElementsByTagName("percentile")[0].childNodes[0].nodeValue;
var sellPrice = data.getElementsByTagName("percentile")[1].childNodes[0].nodeValue;
//This is where the buy / sell prices get updated
ores[oreType].marketBuyPrice = buyPrice;
ores[oreType].marketSellPrice = sellPrice;
})
}
如果我随后使用以下命令查看整个数组,则此方法有效:
console.log(ores);
返回:
{Hemorphite: {…}, Veldspar: {…}}
Hemorphite: {name: "Hemorphite", marketRef: "1231", marketBuyPrice: "1004.87", marketSellPrice: "1678.93"}
Veldspar: {name: "Veldspar", marketRef: "1230", marketBuyPrice: "12.00", marketSellPrice: "20.70"}
但是,如果我尝试查看特定的更新条目:
console.log(ores[oreType].marketBuyPrice);
它返回空的,就好像它还没有更新一样,当查看上面的整个对象时,它显示它已更新。
谁能提供任何关于为什么会这样的见解。
非常感谢
//Update
const ores = {
Hemorphite :{ name: 'Hemorphite', marketRef: '1231', marketBuyPrice: '', marketSellPrice: '', refines: {isogen: '2.4', nocxium: '9'} },
Veldspar :{ name: 'Veldspar', marketRef: '1230', marketBuyPrice: '', marketSellPrice: ''},
};
const minerals = {
Hemorphite :{ name: 'Hemorphite', marketRef: '1231', marketBuyPrice: '', marketSellPrice: '',},
Veldspar :{ name: 'Veldspar', marketRef: '1230', marketBuyPrice: '', marketSellPrice: '',},
};
function getMarketPrices(oreType, id) {
var marketUrl = 'https://api.evemarketer.com/ec/marketstat?typeid='+id+'&usesystem=30000142';
fetch(marketUrl)
.then(response => response.text())
.then(str => (new DOMParser()).parseFromString(str, "text/xml"))
.then(data => {
var buyPrice = data.getElementsByTagName("percentile")[0].childNodes[0].nodeValue;
var sellPrice = data.getElementsByTagName("percentile")[1].childNodes[0].nodeValue;
ores[oreType].marketBuyPrice = buyPrice;
ores[oreType].marketSellPrice = sellPrice;
})
}
console.log(ores);
console.log((ores['Veldspar'].marketBuyPrice));
【问题讨论】:
-
你最好直接使用 EVE api:esi.evetech.net/ui。 Evemarketeer 是一个独立的项目。给我写电报@aka_darth,我会帮你的
标签: javascript arrays object fetch