Home > Back-end >  Move key/value in object array to other array by matched item
Move key/value in object array to other array by matched item

Time:12-07

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"
  }
]

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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.

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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

Code:

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

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

console.log(restult);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related