首页 > 解决方案 > JS 中的 Array(parameter).fill().map 是什么?

问题描述

import './Product.css'

function Product({ title, image, price, rating }) {
  return (
      <div className="product">
          <div className="product__info">
              <p>{title}</p>
              <p className="product__price">
                  <small>$</small>
                  <strong>{price}</strong>
              </p>
              <div className="product__rating">
                  {Array(rating)
                      .fill()
                      .map((_, i) => (
                          <p></p>
                      ))}
              </div>
          </div>
          <img src={image} alt=' ' />
          <button>Add to Basket</button>
      </div>
  )
}

export default Product;

好吧,我正在浏览一个项目的 Youtube 教程。我不明白该声明的{Array(rating).fill().map(_, i)=>(<p>*<p>))}作用。我知道什么fill()是单独做的,什么map()是单独做的。但是我不明白_map 函数中传递的参数。当我试图总结所有正在发生的事情时,我无法掌握正在发生的事情以及如何发生的事情。如果有人需要更多代码,请告诉我。谢谢你。

标签: javascriptreactjs

解决方案


推荐阅读