0

Javascript:

$(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();
});

The first function creates an internal array from my .JSON file and the second function creates an internal array from the URL.

In the first array oreArray, an object looks like this:

{ id: 1234, name: "Title" }

In the second array priceArray, an object looks like this:

{ type_id: 1234, average_price: 56.34 }

My oreArray has 48 objects and unfortunately the priceArray has about 11,000 objects. I need to create a new array by comparing the two arrays and building new objects, where the ID's match. So for example objects in newArray would look like:

{ id: 1234, name: "Title", average_price: 56.34 }

Basically I'm having trouble figuring out the logic for:

  • For each object in oreArray, find the object with the same ID value in priceArray and append the new array with a new object using values from both arrays.

4 Answers 4

1

I would do it this way:

const ores = "../js/json/oreList.json",
      priceURL = "https://esi.tech.ccp.is/latest/markets/prices/?datasource=tranquility";

let oreArray,
    priceArray,
    joinedArray = [];

function getOres() {
    $.getJSON(ores, function(ores) {
        oreArray = ores;
        getPrices();
    });
}

function getPrices() {
    $.getJSON(priceURL, function(prices) {
         priceArray = prices;
         joinPrices();
    });
}

function joinPrices() {
    oreArray.forEach(function(ore) {
        var matchingPrice = getMatchingPrice(ore);
        if(matchingPrice !== false) {
            joinedArray.push({
                id: ore.id,
                name: ore.name,
                average_price: matchingPrice.average_price
            });
        }
    });
}

function getMatchingPrice(ore) {
    for(var i=0; i<priceArray.length; i++) {
        if(priceArray[i].type_id === ore.id) {
            return priceArray[i];
        }
    }
    return false;
}

getOres();
Sign up to request clarification or add additional context in comments.

Comments

1

I think that a good way to approach this problem is by changing the data structure of the average prices a little bit.

Instead of having them in an array, where each item has type_id and average_price field, you might want to consider using an object to store them, where the key is the type_id and the value is the average_price.

To be more concrete, you can replace:

prices.forEach(function(data) {
    priceArray.push(data);
});

With:

const pricesMap = {};
prices.forEach(price => {
  pricesMap[price.type_id] = price.average_price
});

And when looping on the oreArray, you can access each product's average_price by simply referring to pricesMap[ore.id]

You can check out this JSBin: http://jsbin.com/fogayaqexe/edit?js,console

Comments

1

You can use reduce to loop over each oreArr item and collect the data you need in the accumulator:

var oreArr=[
    { id: 1234, name: "Title" },
    { id: 2234, name: "2Title" },
]
var priceArr= [
    { type_id: 1234, average_price: 56.34 },
    { type_id: 2234, average_price: 256.34 },
    { type_id: 3234, average_price: 56.34 },
    { type_id: 4234, average_price: 56.34 },
]

var resArr = oreArr.reduce((ac,x) => {
  var priceMatch = priceArr.find( z => z.type_id === x.id )
  if(! priceMatch)
    return ac //bail out if no priceMatch found
  
  var res = Object.assign({}, x, priceMatch)
  ac.push(res)
  return ac
},[])

console.log(resArr)
  

other methods used:

Comments

0

I suggest you to change your small json as object

eg : '{"1234":{"id": 1234, "name": "Title" }}';

var json = '{"1234":{"id": 1234, "name": "Title" }}';
oreArray = JSON.parse(json);
alert(oreArray['1234'].name); // oreArray[priceArraySingle.id].name

we can easily match priceArray id with oreArray.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.