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

174
Visualizações
The Object.fromEntires() change the sequence of the values in an array when key is numerical-- Javascript

I have a array:

arr = [ ['1', 'a'], ['12', 'b'], ['2', 'c'] ];

I'm using Object.fromEntries() to convert the array into objects with key/value pair. But the problem is Object.fromEntries() seems to changing the sequence of my array based on the key, eg: I want my above array to be converted into object with the same sequnce as shown in the code:

expected o/p:

{1: 'a', 12: 'b', 2: 'c'}

Actual o/p from the fromEntires()

{1: 'a', 2: 'c', 12: 'b'}

any idea why does the function do that and if there is a way to avoid this and render the same sequcne as provided in the array?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

The order of properties in JavaScript objects is not guaranteed to be the insertion order. In particular, keys that parse as integers (such as in your case) will not respect insertion order.

If you need to guarantee order, try using a Map instead:

let arr = [ ['1', 'a'], ['12', 'b'], ['2', 'c'] ];
const map = new Map();
for(let value of arr){
 map.set(value[0], value[1])
}
about 4 years ago · Santiago Gelvez 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