[英]How do I loop through and add key values to values
我希望能夠遍歷此對象並將“鍵”值添加到值中,以便它可以輕松用於項目。 我正在從網站上抓取數據以獲取數據 我已經能夠獲取數據,但我需要對其進行格式化,這是我的代碼,我一直在思考下一步是什么。
我試圖讓它看起來像這樣
例子
服務器:“USSouth2”
費用:100
多少:56
時間:28
代碼
let options = {
url: 'https://www.realmeye.com/items/misc/',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
}
}
var result = [];
request(options, function(err, resp, html) {
if (!err) {
const $ = cheerio.load(html);
let servers = $('.cheapest-server').attr('data-alternatives');
servers.forEach(function(nexus) {
result.push({
servername: nexus[0],
cost: nexus[1],
howmuch: nexus[2],
time: nexus[3]
})
})
}
})
JSON 對象(這可能會因網站數據而異)
["USSouth2 Nexus",100,56,28],["EUSouth Nexus",100,62,25],["AsiaSouthEast Nexus",100,58,25],["EUNorth2 Nexus",100,64,24],["EUEast Nexus",100,55,24],["USWest2 Nexus",100,73,23],["USMidWest2 Nexus",100,53,21],["USEast2 Nexus",100,98,17],["EUWest Nexus",100,66,11],["EUSouthWest Nexus",100,86,10],["USNorthWest Nexus",100,87,9],["USSouthWest Nexus",100,67,9],["EUWest2 Nexus",100,89,8],["USWest Nexus",100,66,8],["USSouth Nexus",100,54,7],["USMidWest Nexus",100,90,6],["USSouth3 Nexus",100,82,6],["USEast Nexus",100,65,1]]
我收到此錯誤TypeError: servers.forEach is not a function
我已經更新了代碼片段。 請立即檢查。
let servers = [["USSouth2 Nexus",100,56,28],["EUSouth Nexus",100,62,25],["AsiaSouthEast Nexus",100,58,25],["EUNorth2 Nexus",100,64,24],["EUEast Nexus",100,55,24],["USWest2 Nexus",100,73,23],["USMidWest2 Nexus",100,53,21],["USEast2 Nexus",100,98,17],["EUWest Nexus",100,66,11],["EUSouthWest Nexus",100,86,10],["USNorthWest Nexus",100,87,9],["USSouthWest Nexus",100,67,9],["EUWest2 Nexus",100,89,8],["USWest Nexus",100,66,8],["USSouth Nexus",100,54,7],["USMidWest Nexus",100,90,6],["USSouth3 Nexus",100,82,6],["USEast Nexus",100,65,1]]; var result = []; servers.forEach(function(nexus) { result.push({ servername: nexus[0], cost: nexus[1], howmuch: nexus[2], time: nexus[3] }); }); console.log(result);
我已經修復它我忘了解析 JSON 這里是通過創建一個新變量的答案
let jsonData = JSON.parse(servers);
好的,所以如果你確定這四個鍵不會改變,那么做這樣的事情。
創建一個具有四個鍵的數組,按出現在單個 JSON 數組值中的順序。 使用數組的map方法遍歷數組並操作每個值,使用 the 和reduce方法將數組轉換為對象。
查看下面的示例以了解它的作用。
// Your JSON. const json = [["USSouth2 Nexus",100,56,28], ["EUSouth Nexus",100,62,25]]; // The four keys of the object. In order of the JSON array. const keys = ['servername', 'cost', 'howmuch', 'time']; // Loop over servers and return an object reduces from an array. const servers = json.map(server => server.reduce((acc, cur, i) => { let key = keys[i]; acc[key]= cur; return acc; }, {})); console.log(servers);
首先,您需要檢查服務器是否已發送,如果是,則為數組 anf 的形式,然后遍歷服務器並獲取您的數據
const servers = $('.cheapest-server').attr('data-alternatives');
if (servers && Array.isArray(servers) && servers.length) {
const result = [];
for (let i = 0; i < servers.lenght; i += 1) {
const entity = servers[i];
const objBuilder = {
servername: entity[0] || null,
cost: entity[1] || null,
howmuch: entity[2] || null,
time: entity[3] || null,
};
result.push(objBuilder);
}
} else {
// Error handling or pass an empty array
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.