1

I have two JSON files.

one looks like this:

    {
base: "EUR",
date: "2019-01-30",

rates: {

    USD: 1.1429,
    AND MORE....
    ...
    ...

}
}

and another one that looks like this:

{
results: {
USD: {
    currencyName: "United States Dollar",
    currencySymbol: "$",
    id: "USD"
}
}

I want to combine them together and get an object that looks like this:

{
results: {
USD: {
    currencyName: "United States Dollar",
    currencySymbol: "$",
    id: "USD",
    value: 1.1429   <====== the change
}
}

I couldn't find anything similar here... and have no idea how to begin

I am getting the values from these links:

https://free.currencyconverterapi.com/api/v6/currencies

https://api.exchangeratesapi.io/latest

with fetch function:

function getdataFromApiAsync() {
  return fetch('https://api.exchangeratesapi.io/latest')
    .then((response) => response.json())
    .then((responseJson) => {
      return responseJson.rates;
    })
    .catch((error) => {
      console.error(error);
    });
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Dor Lugasi-Gal
  • 1,430
  • 13
  • 35

1 Answers1

1

Assuming that your results object has an interface sort of like this:

{
  [key: string]: {
    currencyName: string;
    currencySymbol: string;
    id: string;
    value?: number;
  }
}

where the id here always matches to a key of the rates object, then you would probably want to loop through the values of this results object and add a value from the other object's rates, like this:

Object.values(results).forEach(result => {
  const value = rates[result.id];
  if (value) {
    result.value = value;
  }
});
Scrimothy
  • 2,536
  • 14
  • 23