1

const countryList = [{"countryId":1,"countryQuote":"USDKRW","countryCode":"KRW","countryName":"\uD55C\uAD6D"},{"countryId":2,"countryQuote":"USDJPY","countryCode":"JPY","countryName":"\uC77C\uBCF8"},{"countryId":3,"countryQuote":"USDPHP","countryCode":"PHP","countryName":"\uD544\uB9AC\uD540"}];
const currencyQuoteList = [{"countryQuote":"USDKRW","currencyRate":1162.685028},{"countryQuote":"USDJPY","currencyRate":104.40402},{"countryQuote":"USDPHP","currencyRate":48.480296}];

let itemId = 1;
let countryQuote;
let countryRate;

countryList.forEach(element => {
    if(itemId == element.countryId) {
        countryQuote = element.countryQuote;
    }
});
console.log("countryQuote : " + countryQuote);

currencyQuoteList.forEach(element => {
    if(countryQuote == element.countryQuote) {
        countryRate = element.currencyRate;
    }
})
console.log("countryRate : " + countryRate);

I would like to find currencyRate using itemId.

Const values are given by the server.

I have a countryId in HTML, and I'd like to find currencyRate With these 2 arrays.

Each countryQuote key in arrays would be joinable.

I just have to find currencyRate using brute-force searching, but I would like to improve this code.

What should do I search for it?

Christopher
  • 71
  • 1
  • 8

4 Answers4

3

Does this solution can suit your needs?

Use find instead of forEach :)

I have added a little bit of checks for undefined values.

const countryList = [{"countryId":1,"countryQuote":"USDKRW","countryCode":"KRW","countryName":"\uD55C\uAD6D"},{"countryId":2,"countryQuote":"USDJPY","countryCode":"JPY","countryName":"\uC77C\uBCF8"},{"countryId":3,"countryQuote":"USDPHP","countryCode":"PHP","countryName":"\uD544\uB9AC\uD540"}];
const currencyQuoteList = [{"countryQuote":"USDKRW","currencyRate":1162.685028},{"countryQuote":"USDJPY","currencyRate":104.40402},{"countryQuote":"USDPHP","currencyRate":48.480296}];

// The item identifier
const itemId = 1;

// Search country using itemId
const country = countryList.find(c => c.countryId === itemId);

if(country !== undefined) {
  // Country found

  // Search a match in currencyQuote using countryQuote found
  const currency = currencyQuoteList.find(c => c.countryQuote === country.countryQuote);
  
  if(currency !== undefined) {
    // Currency found !!!
    console.log(`Country: ${country.countryQuote}`)
    console.log(`Currency Rate: ${currency.currencyRate}`);
  } else {
    // Currency not found
    console.log("Invalid countryQuote :(");
  }
} else {
  // Country not found
  console.log("Invalid itemId :(");
}

PS: From this: "Since ES6 there is the native find method for arrays; this stops enumerating the array once it finds the first match and returns the value."

So, using find is far more efficient (if found) rather than check every single element in the array.

Carlo Corradini
  • 2,927
  • 2
  • 18
  • 24
  • 1
    My answer used a `filter()`, but I think your `find()` solution is better here. Upvoted for that! – Marc Sep 19 '20 at 15:20
1

Javascript has a built-in filter function for Arrays. Use it.

const countryList = [{"countryId":1,"countryQuote":"USDKRW","countryCode":"KRW","countryName":"\uD55C\uAD6D"},{"countryId":2,"countryQuote":"USDJPY","countryCode":"JPY","countryName":"\uC77C\uBCF8"},{"countryId":3,"countryQuote":"USDPHP","countryCode":"PHP","countryName":"\uD544\uB9AC\uD540"}];
const currencyQuoteList = [{"countryQuote":"USDKRW","currencyRate":1162.685028},{"countryQuote":"USDJPY","currencyRate":104.40402},{"countryQuote":"USDPHP","currencyRate":48.480296}];

let itemId = 1;
//let countryQuote;
let countryRate;

//this one line replaces your first forEach loop
let country = countryList.filter(countryCode => countryCode.countryId == itemId)[0];

//show result of first filter
console.log("country:");
console.log(country);

//this one line replaces your 2nd forEach loop
let quote = currencyQuoteList.filter(element => element.countryQuote == country.countryQuote)[0];
countryRate = quote.currencyRate;

//show result of 2nd filter
console.log("countryRate : " + countryRate);
Marc
  • 11,403
  • 2
  • 35
  • 45
1

With the JavaScript filter function you could simply filter the country with the specific countryId from your countryList array. Then filter the currencyQuoteList by countryQuote to add the currencyRate property to your country object.

e.g. let's suppose that countryId is 2:

const countryList = [{
  "countryId": 1,
  "countryQuote": "USDKRW",
  "countryCode": "KRW",
  "countryName": "\uD55C\uAD6D"
}, {
  "countryId": 2,
  "countryQuote": "USDJPY",
  "countryCode": "JPY",
  "countryName": "\uC77C\uBCF8"
}, {
  "countryId": 3,
  "countryQuote": "USDPHP",
  "countryCode": "PHP",
  "countryName": "\uD544\uB9AC\uD540"
}];
const currencyQuoteList = [{
  "countryQuote": "USDKRW",
  "currencyRate": 1162.685028
}, {
  "countryQuote": "USDJPY",
  "currencyRate": 104.40402
}, {
  "countryQuote": "USDPHP",
  "currencyRate": 48.480296
}];
let countryId = 2;
let country = countryList.filter(c => c.countryId == countryId)[0];
country.currencyRate = currencyQuoteList.filter(cq => cq.countryQuote == country.countryQuote)[0].currencyRate;
console.log(country);
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
1

I would do this in a little different way that first join both the arrays and create a single array with the consolidated details using Array.reduce and then find based on countryId

const countryList = [{countryId:1,countryQuote:'USDKRW',countryCode:'KRW',countryName:'\uD55C\uAD6D',},{countryId:2,countryQuote:'USDJPY',countryCode:'JPY',countryName:'\uC77C\uBCF8',},{countryId:3,countryQuote:'USDPHP',countryCode:'PHP',countryName:'\uD544\uB9AC\uD540',},];
const currencyQuoteList = [{countryQuote:'USDKRW',currencyRate:1162.685028},{countryQuote:'USDJPY',currencyRate:104.40402},{countryQuote:'USDPHP',currencyRate:48.480296},];


const formatData = data => {
  const finalRes = data.reduce((res, {
    countryQuote,
    ...rest
  }) => {
    res[countryQuote] = {
      ...res[countryQuote],
      ...rest,
    };

    return res;
  }, {});

  return Object.values(finalRes);
};

const formattedData = formatData([...countryList, ...currencyQuoteList]);
//console.log(formattedData);

let itemId = 1;

const getItemById = (data, id) => data.find(({countryId}) => countryId === id)

console.log("ID = 1 then currencyRate: ",getItemById(formattedData, itemId)?.currencyRate);
console.log("ID = 2 then currencyRate: ", getItemById(formattedData, 2)?.currencyRate);
Nithish
  • 5,393
  • 2
  • 9
  • 24