1

I have two arrays with objects containing countries and their country code. One of the arrays have the countries in English and also have key with the country's isoCode. It looks like:

[{
 "name": "Denmark",
 "dialCode": "+45",
 "isoCode": "DK",
},
{
 "name": "Germany",
 "dialCode": "+49",
 "isoCode": "DE",
}]

The other array has the country names in Danish and the dialcode but without the isoCode.

[{
 "name": "Danmark",
 "dialCode": "+45",
},
{
 "name": "Tyskland",
 "dialCode": "+49",
}]

Now I want to move/copy the isoCode from the English array to the Danish, matched by the dialCode. But I am a little lost on how to do this.

I have a matching with this

let result = this.dkList.filter(country1 => this.countryList.some(country2 => country2.dialCode === country1.dialCode.replace('00 ', '+').trim()))

My expected result should be

[{
    "name": "Danmark",
    "dialCode": "+45",
    "isoCode": "DK"
  },
  {
    "name": "Tyskland",
    "dialCode": "+49",
    "isoCode": "DE"
  }
]
4
  • Your data is not an array. Its an object Commented Dec 7, 2021 at 8:41
  • @Nitheesh "two arrays with objects"... Commented Dec 7, 2021 at 8:42
  • Are you also able to provide an example of your expected result with the above data? Commented Dec 7, 2021 at 8:47
  • 1
    @NickParsons - I have edited my question Commented Dec 7, 2021 at 8:50

5 Answers 5

2

The .filter() method is used to remove items from an array, which isn't what you're looking to do here, instead, you're looking to map (ie: transform) your data.

You can first create a Map called isoLookup from your first array that creates keys based. on the dialCode and values based on the isoCode in your array. You can then you this Map to obtain the ISO code using a dialCode. Once you have created the Map lookup, you can use .map() on your second arr2 to map your objects to new objects with an isoCode property, that holds a value obtained from the Map lookup:

const arr1 = [{ "name": "Denmark", "dialCode": "+45", "isoCode": "DK", }, { "name": "Germany", "dialCode": "+49", "isoCode": "DE", }];
const arr2 = [{ "name": "Danmark", "dialCode": "+45", }, { "name": "Tyskland", "dialCode": "+49", }];

const isoLookup = new Map(arr1.map(obj => [obj.dialCode, obj.isoCode]));
const res = arr2.map(obj => ({...obj, isoCode: isoLookup.get(obj.dialCode)}));
console.log(res);

Creating a Map to serve as a lookup allows your code to scale nicely, as apposed to performing the search through arr1 for each iteration of your .map() method.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Will look into this! I am using filter to remove countries from the english array that can't be found in the danish array, like for example "Antarctica".
@MortenHagh ah, got you, I mistook it for code that wasn't working. All good then, .map() for this should be what is needed
Got your code to work perfectly with mine! Thank you!
0

Just go through all array elements, compare their dialCodes and copy isoCodes to danish version.

const en = [...]; // array with english data
const den = [...]; // array with danish data

// it will work if en.length = den.length
for (let i = 0; i < en.length; i++) {
  if (den[i].dialCode === en[i].dialCode) { // compare en & den dial codes
    den[i].isoCode = en[i].isoCode;
  }
}

Comments

0
var en = []
var den = []
den.forEach(eni => {
    en.forEach(deni => {
        if (eni.dialcode == deni.dialcode) {
            den.dialcode = en.dialcode
        }
    })
})

now you should have the array in var den

Comments

0

One line solution, if don't mind

const arr1 = [{ "name": "Denmark", "dialCode": "+45", "isoCode": "DK", }, { "name": "Germany", "dialCode": "+49", "isoCode": "DE", }];
const arr2 = [{ "name": "Danmark", "dialCode": "+45", }, { "name": "Tyskland", "dialCode": "+49", }];

result = arr1.map(({ dialCode, isoCode }) => ({ name: arr2.find((o) => dialCode === o.dialCode).name, dialCode, isoCode }));

console.log(result);

Comments

0

Because both lists contain dialCode: with Array.prototype.reduce() you can create a hash from arr1 based on the dialCode property and finally get the result out of Array.prototype.map() arr2 list

Code:

const arr1 = [{name: 'Denmark',dialCode: '+45',isoCode: 'DK',},{name: 'Germany',dialCode: '+49',isoCode: 'DE'}]
const arr2 = [{name: ' Danmark',dialCode: '+45',},{name: 'Tyskland',dialCode: '+49'}]

const hash = arr1.reduce((a, c) => ((a[c.dialCode] = c.isoCode), a), {});
const restult = arr2.map(c => ({ ...c, isoCode: hash[c.dialCode] }));

console.log(restult);

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.