0
\$\begingroup\$

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);

Live Demo

\$\endgroup\$

3 Answers 3

2
\$\begingroup\$

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).

\$\endgroup\$
3
  • \$\begingroup\$ can you place your working code please? \$\endgroup\$
    – 3gwebtrain
    Commented Nov 10, 2022 at 15:18
  • \$\begingroup\$ @3gwebtrain like that? \$\endgroup\$
    – slepic
    Commented 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
    Commented Nov 11, 2022 at 6:25
0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\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\$ Commented 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
    Commented Nov 11, 2022 at 14:23
0
\$\begingroup\$

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);

\$\endgroup\$
2
  • \$\begingroup\$ Better to use a set of the ids. See my answer. \$\endgroup\$
    – slepic
    Commented 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\$ Commented Nov 11, 2022 at 8:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.