JavaScript Array Map

未能成功加载,请稍后再试
0/0

Another very useful and powerful method we have in modern JavaScript is the map method.

With this method, we can map each item in an array to something else.

So following from the example in the last lecture, we have an array of positive numbers.

Let's say we want to construct some HTML markup using the elements in this array.

We call filtered dot map and once again, we need to pass a callback function here This function just like the function that we passed to the filter method can have three parameters value index and array So in this case again, we're gonna work only with the value so we can pass a function or an arrow function so we get a number and Then we want to map this to some HTML markup So we can add a string in an opening list item element.

Then we add the number and finally the closing list item element.

So with this markup we can display each number using a bullet point.

Of course we need to put these inside of a UL element and I'm going to show you that in a second.

So let's see the result of the map method.

I'm going to store the result in a constant called items and now let's display this on the console.

下载全新《每日英语听力》客户端,查看完整内容