Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!