Javascriptを:のJavascriptのforEach
$(document).ready(function() {
const ores = "../js/json/oreList.json";
const priceURL = "https://esi.tech.ccp.is/latest/markets/prices/?datasource=tranquility";
let oreArray = [];
let priceArray = [];
let total = 0;
// Retrieve list of ores
function getOres() {
$.getJSON(ores, function(ores) {
ores.forEach(function(ore) {
total++;
if (total === 48) {
getPrices();
}
oreArray.push(ore);
});
});
}
// Retrieve all items & prices via API
function getPrices() {
$.getJSON(priceURL, function(prices) {
prices.forEach(function(data) {
priceArray.push(data);
console.log(data);
});
});
}
getOres();
});
は、最初の関数は、私の.JSONファイルから内部の配列を作成し、第二の機能は、URLからの内部配列を作成します。二番目の配列priceArrayで
{ id: 1234, name: "Title" }
、オブジェクトは次のようになります:
{ type_id: 1234, average_price: 56.34 }
マイoreArray
48個のオブジェクトと残念ながら持つオブジェクトは、次のように見える最初の配列oreArray
において
priceArray
には約11,000のオブジェクトがあります。 2つの配列を比較し、IDの一致する新しいオブジェクトを構築することによって、新しい配列を作成する必要があります。
{ id: 1234, name: "Title", average_price: 56.34 }
基本的に私はトラブルのためのロジックを考え出すことだ:だから、たとえば次のようになりNEWARRAY内のオブジェクト
oreArray内の各オブジェクトに対して- を、中に同じID値を持つオブジェクトを見つけますpriceArrayを作成し、両方の配列の値を使用して新しい配列を新しいオブジェクトに追加します。