'How to return an array of numbers that represent lengths of elements string?
If an array was: ['hey', 'you', 'muddy'] The expected output should be: [3, 3, 5]
This is what I have so far:
function lengths(arr) {
numbersArray = [];
for (var i = 0; i < arr.length; i++) {
numbersArray = arr[i].length;
}
}
Any help would be much appreciated.
Solution 1:[1]
const getStringLength = (arr) => {
return arr.map((item) => item.length);
}
console.log(getStringLength(['hello', 'world']));
Solution 2:[2]
function lengths(arr) {
return arr.map(el => el.length)
}
Array.map is commonly used in such things. Docs
Solution 3:[3]
Try this.
const arrStrLen =(a)=>a.map((i)=>i.length);
console.log(arrStrLen(['hey', 'you', 'muddy']));
console.log(arrStrLen(['How','to','return']));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Parvesh Kumar |
| Solution 2 | ??????? A. |
| Solution 3 | Mohsen Alyafei |
