Javascript 如何使用函数和文本映射

Javascript 如何使用函数和文本映射,javascript,reactjs,function,dictionary,Javascript,Reactjs,Function,Dictionary,我想解释一下我今天的问题 目前我的地图,一切正常 我的问题,我希望我可以在其中创建一个div,以便显示total的结果 很遗憾,我不能 你知道如何解决这个问题吗 {this.props.items.map(item => { const product = this.props.items; let total = 0; console.log(total) console.log('yolo',product) console.log('yolo array',produ

我想解释一下我今天的问题

目前我的地图,一切正常

我的问题,我希望我可以在其中创建一个div,以便显示total的结果 很遗憾,我不能

你知道如何解决这个问题吗

 {this.props.items.map(item => { 
 const product = this.props.items;
 let total = 0;

 console.log(total)
 console.log('yolo',product)
 console.log('yolo array',product[0].quantity)

for (let i = 0 ; i<product.length;i++){
console.log('products',product[i].quantity) 
total = total + product[i].quantity;                        
}
} 
)}
{this.props.items.map(item=>{
const product=this.props.items;
设total=0;
console.log(总计)
console.log('yolo',产品)
console.log('yolo数组',产品[0]。数量)

对于(设i=0;i即使要使用total渲染某些内容,使用此方法,您也可以在
props.items
中为每个
项目
渲染一个“total item”。这里要使用的是
reduce
方法。下面是一个示例,其中包含一个非常基本的div,但您可以决定使用什么来替代它

<div>
{this.props.items.reduce((total,item) => item.quantity + total, 0)}
</div>

{this.props.items.reduce((总数,item)=>item.quantity+total,0)}

reduce
方法所做的是从元素列表中返回一个值。第二个参数是您的初始值,而第一个参数是将在每次迭代中执行的回调

这是否回答了您的问题?