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);
    });
}
13
  • how to handle those "AND MORE..." cases? Commented Jan 30, 2019 at 17:10
  • so you have to loop over the one and add it to the other.... going to be loops.... Commented Jan 30, 2019 at 17:12
  • @messerbill yes. Commented Jan 30, 2019 at 17:13
  • What you posted as "JSON" is not JSON at all. There's no such thing as a "JSON Object" Commented Jan 30, 2019 at 17:13
  • 1
    As epascarello points out - loop over one and add it to the other. Best of luck to you. Commented Jan 30, 2019 at 17:21

1 Answer 1

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;
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.