If you also want to improve performance. And that should only concern you if the lookup array is actually much larger than the one you provided. Consider converting the lookup array to a lookup map where keys would be the value of countryCd
(or possibly create just a set of all countryCd
values). Then you can use @3gwebtrain's code but instead of lookup.find()
you call lookupMap.has(country.codeId)
.do:
const lookupSetcodeIds = new Set(lookup.map((v) => v.countryCd))
const final = countries.filter((c) => lookupSetcodeIds.has(c.codeId))
Let m
be the size of lookup
array and n
be the size of countries
array. Time complexity of your original code, as well as @3gwebtrain's and @An Nguyen's, is O(n ×ばつ m)
. My version is O(n + m)
.
If you also want to improve performance. And that should only concern you if the lookup array is actually much larger than the one you provided. Consider converting the lookup array to a lookup map where keys would be the value of countryCd
(or possibly create just a set of all countryCd
values). Then you can use @3gwebtrain's code but instead of lookup.find()
you call lookupMap.has(country.codeId)
.
const lookupSet = new Set(lookup.map((v) => v.countryCd))
const final = countries.filter((c) => lookupSet.has(c.codeId))
Let m
be the size of lookup
array and n
be the size of countries
array. Time complexity of your original code, as well as @3gwebtrain's and @An Nguyen's, is O(n ×ばつ m)
. My version is O(n + m)
.
If you also want to improve performance. And that should only concern you if the lookup array is actually much larger than the one you provided. Consider converting the lookup array to a lookup map where keys would be the value of countryCd
(or possibly create just a set of all countryCd
values). Then you can do:
const codeIds = new Set(lookup.map((v) => v.countryCd))
const final = countries.filter((c) => codeIds.has(c.codeId))
Let m
be the size of lookup
array and n
be the size of countries
array. Time complexity of your original code, as well as @An Nguyen's, is O(n ×ばつ m)
. My version is O(n + m)
.
If you also want to improve performance. And that should only concern you if the lookup array is actually much larger than the one you provided. Consider converting the lookup array to a lookup map where keys would be the value of countryCd
(or possibly create just a set of all countryCd
values). Then you can use @3gwebtrain's code but instead of lookup.find()
you call lookupMap.has(country.codeId)
.
const lookupSet = new Set(lookup.map((v) => v.countryCd))
const final = countries.filter((c) => lookupSet.has(c.codeId))
Let mm
be the size of look-uplookup
array and nn
be the size of countriescountries
array. Time complexity of your original code, as well as @3gwebtrain's and @An Nguyen's, is O(n times m)O(n ×ばつ m)
. My version is O(n plus m)O(n + m)
.
If you also want to improve performance. And that should only concern you if the lookup array is actually much larger than the one you provided. Consider converting the lookup array to a lookup map where keys would be the value of countryCd
(or possibly create just a set of all countryCd
values). Then you can use @3gwebtrain's code but instead of lookup.find()
you call lookupMap.has(country.codeId)
.
const lookupSet = new Set(lookup.map((v) => v.countryCd))
const final = countries.filter((c) => lookupSet.has(c.codeId))
Let m be the size of look-up array and n be the size of countries array. Time complexity of your original code, as well as @3gwebtrain's and @An Nguyen's, is O(n times m). My version is O(n plus m)
If you also want to improve performance. And that should only concern you if the lookup array is actually much larger than the one you provided. Consider converting the lookup array to a lookup map where keys would be the value of countryCd
(or possibly create just a set of all countryCd
values). Then you can use @3gwebtrain's code but instead of lookup.find()
you call lookupMap.has(country.codeId)
.
const lookupSet = new Set(lookup.map((v) => v.countryCd))
const final = countries.filter((c) => lookupSet.has(c.codeId))
Let m
be the size of lookup
array and n
be the size of countries
array. Time complexity of your original code, as well as @3gwebtrain's and @An Nguyen's, is O(n ×ばつ m)
. My version is O(n + m)
.
If you also want to improve performance. And that should only concern you if the lookup array is actually much larger than the one you provided. Consider converting the lookup array to a lookup map where keys would be the value of countryCd
(or possibly create just a set of all countryCd
values). Then you can use @3gwebtrain's code but instead of lookup.find()
you call lookupMap.has(country.codeId)
.
const lookupSet = new Set(lookup.map((v) => v.countryCd))
const final = countries.filter((c) => lookupSet.has(c.codeId))
Let m be the size of look-up array and n be the size of countries array. Time complexity of your original code, as well as @3gwebtrain's and @An Nguyen's, is O(n times m). My version is O(n plus m)
If you also want to improve performance. And that should only concern you if the lookup array is actually much larger than the one you provided. Consider converting the lookup array to a lookup map where keys would be the value of countryCd
(or possibly create just a set of all countryCd
values). Then you can use @3gwebtrain's code but instead of lookup.find()
you call lookupMap.has(country.codeId)
.
const lookupSet = new Set(lookup.map((v) => v.countryCd))
const final = countries.filter((c) => lookupSet.has(c.codeId))
If you also want to improve performance. And that should only concern you if the lookup array is actually much larger than the one you provided. Consider converting the lookup array to a lookup map where keys would be the value of countryCd
(or possibly create just a set of all countryCd
values). Then you can use @3gwebtrain's code but instead of lookup.find()
you call lookupMap.has(country.codeId)
.
const lookupSet = new Set(lookup.map((v) => v.countryCd))
const final = countries.filter((c) => lookupSet.has(c.codeId))
Let m be the size of look-up array and n be the size of countries array. Time complexity of your original code, as well as @3gwebtrain's and @An Nguyen's, is O(n times m). My version is O(n plus m)