I am doing the filter declaring a final
array. it seems that the codes are much. it can be shortened?
code :
const lookup = [
{
hfsUserCountryId: 2895,
countryCd: 140,
},
{
hfsUserCountryId: 2895,
countryCd: 1301,
},
];
const countries = [
{
"defaultOrder" : 0,
"label" : "data.Code.Country_Cd.Code_Desc.US",
"codeId" : 140,
"code" : "US",
"codeDesc" : "USA",
"codeQualifier" : "Country_Cd"
},
{
"defaultOrder" : null,
"label" : "data.Code.Country_Cd.Code_Desc.AE",
"codeId" : 1301,
"code" : "AE",
"codeDesc" : "United Arab Emirates",
"codeQualifier" : "Country_Cd"
},
{
"defaultOrder" : 1,
"label" : "data.Code.Country_Cd.Code_Desc.AL",
"codeId" : 1086,
"code" : "AL",
"codeDesc" : "Albania",
"codeQualifier" : "Country_Cd"
}, {
"defaultOrder" : null,
"label" : "data.Code.Country_Cd.Code_Desc.DZ",
"codeId" : 2615,
"code" : "DZ",
"codeDesc" : "Algeria",
"codeQualifier" : "Country_Cd"
}
];
const final = []
const result = countries.filter(item =>{
const {codeId} = item;
const r = lookup.filter(look => {
if( look.countryCd === codeId){
final.push(item)
}
} );
});
console.log('reuslt', final);
3 Answers 3
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)
.
-
\$\begingroup\$ can you place your working code please? \$\endgroup\$3gwebtrain– 3gwebtrain2022年11月10日 15:18:34 +00:00Commented Nov 10, 2022 at 15:18
-
\$\begingroup\$ @3gwebtrain like that? \$\endgroup\$slepic– slepic2022年11月11日 04:52:30 +00:00Commented Nov 11, 2022 at 4:52
-
\$\begingroup\$ BTW here is a link to a question which includes the very same problem. I am writing there a bit more deeper about the specifics of the implementation under various circumstances. codereview.stackexchange.com/a/281071/212100 \$\endgroup\$slepic– slepic2022年11月11日 06:25:47 +00:00Commented Nov 11, 2022 at 6:25
I tried like this:
const c = countries.filter((county) => {
return lookup.find(v => v.countryCd === county.codeId ?? county)
});
works. if any one have other thoughts please share.
-
\$\begingroup\$ You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer. \$\endgroup\$Toby Speight– Toby Speight2022年11月11日 08:43:21 +00:00Commented Nov 11, 2022 at 8:43
-
\$\begingroup\$ Lol, only now I realized you're the same person as who asked the original question. I had to rephrase my answer to make it sound less schizophrenic :D \$\endgroup\$slepic– slepic2022年11月11日 14:23:54 +00:00Commented Nov 11, 2022 at 14:23
Just need to use a filter function with array includes function.
const lookup = [
{
hfsUserCountryId: 2895,
countryCd: 140,
},
{
hfsUserCountryId: 2895,
countryCd: 1301,
},
];
const countries = [
{
"defaultOrder" : 0,
"label" : "data.Code.Country_Cd.Code_Desc.US",
"codeId" : 140,
"code" : "US",
"codeDesc" : "USA",
"codeQualifier" : "Country_Cd"
},
{
"defaultOrder" : null,
"label" : "data.Code.Country_Cd.Code_Desc.AE",
"codeId" : 1301,
"code" : "AE",
"codeDesc" : "United Arab Emirates",
"codeQualifier" : "Country_Cd"
},
{
"defaultOrder" : 1,
"label" : "data.Code.Country_Cd.Code_Desc.AL",
"codeId" : 1086,
"code" : "AL",
"codeDesc" : "Albania",
"codeQualifier" : "Country_Cd"
}, {
"defaultOrder" : null,
"label" : "data.Code.Country_Cd.Code_Desc.DZ",
"codeId" : 2615,
"code" : "DZ",
"codeDesc" : "Algeria",
"codeQualifier" : "Country_Cd"
}
];
// const lookupIds = [1301, 140];
const lookupIds = lookup.map(({countryCd}) => countryCd);
const res = countries.filter(country => lookupIds.includes(country.codeId));
console.log('reuslt', res);
-
\$\begingroup\$ Better to use a set of the ids. See my answer. \$\endgroup\$slepic– slepic2022年11月11日 05:29:25 +00:00Commented Nov 11, 2022 at 5:29
-
\$\begingroup\$ You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer. \$\endgroup\$Toby Speight– Toby Speight2022年11月11日 08:42:37 +00:00Commented Nov 11, 2022 at 8:42