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

94
Visualizações
Return a string from JSON through Javascript

I'm looking to return the skill below from a variable:

const skillsData = [
  { name: "React", value: 390 },
  { name: "CSS", value: 1200 },
  { name: "JavsScript", value: 540 },
  { name: "HTML", value: 1900 },
  { name: "CSS", value: 1278 },
];

Within a function, I am using the following:

let maxSkills = Math.max.apply(Math, skillsData.map(function(o) { return o.value; }))

This will obviously return a value, which has worked for one case, however what is the correct way to return the following 'name' key on the basis?

Furthermore, what would be a function to sort these in ascending value, so that you return HTML as the last in JSON?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Sort the array by using the sort method.

This will produce the array sorted in descending order:

skillsData.sort((a, b) => b.value - a.value)

and ascending order:

skillsData.sort((a, b) => a.value - b.value)

The following code will print the .name value of the first element in the sorted array. The first element holds the maximum value, as sortedSkillsData is in descending order.

const skillsData = [
  { name: "React", value: 390 },
  { name: "CSS", value: 1200 },
  { name: "JavsScript", value: 540 },
  { name: "HTML", value: 1900 },
  { name: "CSS", value: 1278 },
];

let sortedSkillsData = skillsData.sort((a, b) => b.value - a.value);
let maxSkills = sortedSkillsData[0].name;
console.log(maxSkills);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use sort method and sort in ascending order and then get the last element and then property name from that.

const skillsData = [
  { name: "React", value: 390 },
  { name: "CSS", value: 1200 },
  { name: "JavsScript", value: 540 },
  { name: "HTML", value: 1900 },
  { name: "CSS", value: 1278 },
];

const maxSkills = skillsData.sort((a, b) => a.value - b.value).slice(-1)[0].name
console.log(maxSkills)

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