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

115
Visualizações
How do I return an object of word and its length from an array of words?

Given an array of strings, return an object containing a key for every different string in the array, and the value is that string's length.

wordLen(["a", "bb", "a", "bb"])          → { "bb": 2, "a": 1 }
wordLen(["this", "and", "that", "and"])  → { "that": 4, "and": 3, "this": 4 }
wordLen(["code", "code", "code", "bug"]) → { "code": 4, "bug": 3 }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

var arr = ["a", "bb", "a", "bb"];
var entries = arr.map(x => [x, x.length]);
console.log(Object.fromEntries(entries));

about 4 years ago · Juan Pablo Isaza Relatório

0

You can loop through the array and store each word length in an object with the word itself being the key

function wordLen(arr){
    let words = {};
    arr.forEach(word => words[word] = word.length);
    return words;
}

Non-ES6

function wordLen(arr){
    var words = {};
    arr.forEach(function(word){ words[word] = word.length });
    return words;
}
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