这是一个购物车。
我想检索regularListings中的商品价格,并用它们来计算购物车的总数。
购物车中的第一项' michael'是客户的名字。
如何在JavaScript中完成?
'use strict'
const listing =
(name, price) => ({
name,
price
})
const cart =
(customer, ...items) => ({
customer,
items
})
const listedPrice =
listing =>
name =>
name === listing.name
? listing.price
: 0
const regularListings = [
listing('detergent', 10),
listing('hennessey', 100),
listing('carlo rozzi', 20),
listing('coffee', 5),
listing('cookies', 6),
listing('mountain dew', 2)
]
const carts = [
cart(
'michael',
'coffee',
'hennessey',
'coffee',
'hennessey',
'coffee',
'hennessey'
)
]
答案 0 :(得分:3)
您可以减少regularListings
并计算总数
const regularListings = [
listing('detergent', 10),
listing('hennessey', 100),
listing('carlo rozzi', 20),
listing('coffee', 5),
listing('cookies', 6),
listing('mountain dew', 2)
]
const total = regularListings.reduce((state, item) => state + item, 0);