0

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]
1

2 Answers 2

1

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);

Sign up to request clarification or add additional context in comments.

2 Comments

or shorter: arr.map(({length}) => length);
@Chris yes, my first answer was shorter also like arr.map(element => element.length) but I edited it to make it more readable
0

You can also do this by looping through the array and printing its length

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

for (var i=0;i<arr.length;i++){
console.log(arr[i].length);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.