Reputation: 2113
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"
}
]
Upvotes: 1
Views: 83
Reputation: 19090
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);
Upvotes: 0
Reputation: 4780
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);
Upvotes: 0
Reputation: 21
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
Upvotes: 0
Reputation: 50914
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.
Upvotes: 2
Reputation: 214
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;
}
}
Upvotes: 0