Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
Move key/value in object array to other array by matched item

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"
  }
]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

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;
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda