Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

javascript - Count length of each string in an array

This is my JS code:

var arr = ["the", "quick", "brown", "fox"];
    
console.log(arr.length);

I want an output like this:

[3, 5, 5, 3]
question from:https://stackoverflow.com/questions/65863201/count-length-of-each-string-in-an-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use map function of array to get the new resultant array according to the operation performed for each element in the array

var arr = ["the", "quick", "brown", "fox"];

console.log(arr);

const resultArray = arr.map((element) => {
   return element.length;
});

console.log(resultArray);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...